Skip to content

Commit b76bbd8

Browse files
Script documentation update (#492)
1 parent 835101c commit b76bbd8

File tree

1 file changed

+154
-0
lines changed

1 file changed

+154
-0
lines changed

scripts/data_transfer.js

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
2+
/* Documentation for the DataTransferScript
3+
4+
Overview
5+
6+
The `DataTransferScript` is an Apps Script designed to streamline the process of copying specific data from one Google Sheets tab ("Raw data") to another ("[CLEANED] For import") in the ‘[Public] OpenMobilityData source updates’ google sheet and then updating column C in the ("[CLEANED] For import") tab based on conditions set. The script consists of two main functions: `copyData` and `updateDates`.
7+
8+
Script Functions
9+
10+
1. DataTransferScript():
11+
Purpose: This function serves as the main function that calls the other two functions: `copyData` and `updateDates`.
12+
13+
Function Calls:
14+
copyData(): Copies selected data from the "Raw data" tab to the "[CLEANED] For import" tab in the ‘[Public] OpenMobilityData source updates’ google sheet.
15+
updateDates() : Updates dates in column B of the "[CLEANED] For import" tab based on the presence of data in column C in the ‘[Public] OpenMobilityData source updates’ google sheet.
16+
17+
2. copyData():
18+
19+
Purpose: Copies data from specific columns in the "Raw data" tab to corresponding columns in the "[CLEANED] For import" tab based on a user-specified row range.
20+
21+
Expected Inputs:
22+
User input for the row range (e.g., a single row like 5 or a range like 3:10).
23+
24+
Expected Outputs:
25+
Data from the selected rows and specified columns in the "Raw data" tab is copied to the corresponding columns in the "[CLEANED] For import" tab.
26+
27+
28+
3. updateDates():
29+
30+
Purpose: Updates the date in column B of the "[CLEANED] For import" tab if the corresponding cell in column C is not empty.
31+
32+
Expected Inputs:
33+
The function operates on the data present in columns B and C of the active sheet.
34+
35+
Expected Inputs:
36+
Column B is updated with the current date if the corresponding cell in column C has a value. If column C is empty, the corresponding cell in column B is cleared.
37+
38+
39+
How to run the script:
40+
41+
1. Open the ‘[Public] OpenMobilityData source updates’ google sheet.
42+
2. Go to the "[CLEANED] For import" tab.
43+
3. Click on the “Data Transfer” button.
44+
4. Input desired row number or range you want to copy from the ("Raw data") tab.
45+
5. The `DataTransferScript` validates the input and converts it into a list of row numbers.
46+
6. Specific columns are copied from the source tab ("Raw data") to the destination tab ("[CLEANED] For import") based on the user's input.
47+
7. The copied data is pasted into the appropriate columns starting from the first available row in the destination tab.
48+
8. The `updateDates()` function is called to update the dates in column B of the "[CLEANED] For import" tab based on the presence of data in column C.
49+
9. The `updateDates() script iterates through all rows starting from row 2 (to avoid the header)
50+
10. It checks if each cell in column C has a value.
51+
11. If a value exists, the corresponding cell in column B is updated with the current date.
52+
12. If no value exists in column C, the corresponding cell in column B is cleared.
53+
54+
*/
55+
56+
57+
// Running the Script Code
58+
59+
60+
function DataTransferScript() {
61+
62+
copyData();
63+
64+
// Update dates based on column C
65+
updateDates();
66+
}
67+
68+
function copyData() {
69+
// Get the spreadsheet and source/destination tabs
70+
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
71+
var sourceSheet = spreadsheet.getSheetByName("Raw data");
72+
var destinationSheet = spreadsheet.getSheetByName("[CLEANED] For import");
73+
74+
// Get user input for row range
75+
var ui = SpreadsheetApp.getUi();
76+
var response = ui.prompt('Enter row number or range (e.g., 5 or 3:10):', ui.ButtonSet.OK_CANCEL);
77+
78+
if (response.getSelectedButton() == ui.Button.CANCEL) {
79+
return;
80+
}
81+
82+
var rowRange = response.getResponseText();
83+
84+
// Convert row range to numbers
85+
var rowNumbers = [];
86+
try {
87+
if (rowRange.includes(":")) {
88+
var rangeParts = rowRange.split(":");
89+
var startRow = parseInt(rangeParts[0]);
90+
var endRow = parseInt(rangeParts[1]);
91+
for (var i = startRow; i <= endRow; i++) {
92+
rowNumbers.push(i);
93+
}
94+
} else {
95+
rowNumbers.push(parseInt(rowRange));
96+
}
97+
} catch (error) {
98+
Browser.msgBox("Invalid row range format. Please enter a valid row number or range (e.g., 5 or 3:10).");
99+
return;
100+
}
101+
102+
// columns to copy
103+
var sourceColumns = [3,7,8,9,10,11,12,13,15,19,20,21,22,26,29]; // columns C, F, H, J
104+
var destinationColumns = [3,7,8,9,10,11,12,13,15,19,20,21,22,26,30]; // Matching destination columns
105+
106+
// Validation of column arrays
107+
if (sourceColumns.length !== destinationColumns.length) {
108+
Browser.msgBox("Error: Source and destination column arrays must have the same length.");
109+
return;
110+
}
111+
112+
// Creation of array to hold the values to be copied
113+
var valuesToCopy = [];
114+
115+
// loop over rows and extract values for each column specified
116+
for (var i = 0; i < rowNumbers.length; i++) {
117+
var row = rowNumbers[i];
118+
var rowValues = [];
119+
for (var j = 0; j < sourceColumns.length; j++) {
120+
var sourceColumn = sourceColumns[j];
121+
rowValues.push(sourceSheet.getRange(row, sourceColumn).getValue());
122+
}
123+
valuesToCopy.push(rowValues);
124+
}
125+
126+
// Starting row for the destination range
127+
var destinationStartRow = destinationSheet.getLastRow() + 1;
128+
129+
// loop to paste the values into the correct destination columns
130+
for (var k = 0; k < destinationColumns.length; k++) {
131+
var colValues = valuesToCopy.map(function(row) {
132+
return [row[k]];
133+
});
134+
var destinationColumn = destinationColumns[k];
135+
destinationSheet.getRange(destinationStartRow, destinationColumn, colValues.length, 1).setValues(colValues);
136+
}
137+
}
138+
// function to update column B with current dates based on column C
139+
function updateDates() {
140+
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
141+
var lastRow = sheet.getLastRow();
142+
var columnC = sheet.getRange("C2:C" + lastRow).getValues();
143+
var currentDate = new Date(); // Get today's date
144+
145+
for (var i = 0; i < columnC.length; i++) {
146+
var rowIndex = i + 2;
147+
var targetCell = sheet.getRange(rowIndex, 2);
148+
if (columnC[i][0] !== "") {
149+
targetCell.setValue(currentDate);
150+
} else {
151+
targetCell.clear(); // Clear the cell in column B if the corresponding cell in column C is empty
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)