-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Script function to copy and clean data with date updates #491
Open
AlfredNwolisa
wants to merge
2
commits into
main
Choose a base branch
from
appscript
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
function combinedScript() { | ||
|
||
copyDataToCleanedTab(); | ||
|
||
// Update dates based on column C | ||
updateDatesBasedOnColumnC(); | ||
} | ||
|
||
function copyDataToCleanedTab() { | ||
// Get the spreadsheet and source/destination tabs | ||
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); | ||
var sourceSheet = spreadsheet.getSheetByName("Raw data"); | ||
var destinationSheet = spreadsheet.getSheetByName("[CLEANED] For import"); | ||
|
||
// Get user input for row range | ||
var ui = SpreadsheetApp.getUi(); | ||
var response = ui.prompt('Enter row number or range (e.g., 5 or 3:10):', ui.ButtonSet.OK_CANCEL); | ||
|
||
if (response.getSelectedButton() == ui.Button.CANCEL) { | ||
return; | ||
} | ||
|
||
var rowRange = response.getResponseText(); | ||
|
||
// Convert row range to numbers | ||
var rowNumbers = []; | ||
try { | ||
if (rowRange.includes(":")) { | ||
var rangeParts = rowRange.split(":"); | ||
var startRow = parseInt(rangeParts[0]); | ||
var endRow = parseInt(rangeParts[1]); | ||
for (var i = startRow; i <= endRow; i++) { | ||
rowNumbers.push(i); | ||
} | ||
} else { | ||
rowNumbers.push(parseInt(rowRange)); | ||
} | ||
} catch (error) { | ||
Browser.msgBox("Invalid row range format. Please enter a valid row number or range (e.g., 5 or 3:10)."); | ||
return; | ||
} | ||
|
||
// columns to copy | ||
var sourceColumns = [3,7,8,9,10,11,12,13,15,19,20,21,22,26,29]; // columns C, F, H, J | ||
var destinationColumns = [3,7,8,9,10,11,12,13,15,19,20,21,22,26,30]; // Matching destination columns | ||
|
||
// Validation of column arrays | ||
if (sourceColumns.length !== destinationColumns.length) { | ||
Browser.msgBox("Error: Source and destination column arrays must have the same length."); | ||
return; | ||
} | ||
|
||
// Creation of array to hold the values to be copied | ||
var valuesToCopy = []; | ||
|
||
// loop over rows and extract values for each column specified | ||
for (var i = 0; i < rowNumbers.length; i++) { | ||
var row = rowNumbers[i]; | ||
var rowValues = []; | ||
for (var j = 0; j < sourceColumns.length; j++) { | ||
var sourceColumn = sourceColumns[j]; | ||
rowValues.push(sourceSheet.getRange(row, sourceColumn).getValue()); | ||
} | ||
valuesToCopy.push(rowValues); | ||
} | ||
|
||
// Starting row for the destination range | ||
var destinationStartRow = destinationSheet.getLastRow() + 1; | ||
|
||
// loop to paste the values into the correct destination columns | ||
for (var k = 0; k < destinationColumns.length; k++) { | ||
var colValues = valuesToCopy.map(function(row) { | ||
return [row[k]]; | ||
}); | ||
var destinationColumn = destinationColumns[k]; | ||
destinationSheet.getRange(destinationStartRow, destinationColumn, colValues.length, 1).setValues(colValues); | ||
} | ||
} | ||
|
||
function updateDatesBasedOnColumnC() { | ||
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); // Get the active sheet | ||
var lastRow = sheet.getLastRow(); | ||
var columnC = sheet.getRange("C2:C" + lastRow).getValues(); | ||
var currentDate = new Date(); // Get today's date | ||
|
||
for (var i = 0; i < columnC.length; i++) { | ||
var rowIndex = i + 2; | ||
var targetCell = sheet.getRange(rowIndex, 2); | ||
if (columnC[i][0] !== "") { | ||
targetCell.setValue(currentDate); | ||
} else { | ||
targetCell.clear(); // Clear the cell in column B if the corresponding cell in column C is empty | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a section documenting how this script is used, expected inputs, outputs, etc.