问题
I have a JavaScript code that gets response from API and sets the values on google spreadsheet.
The data that I get are users on a project that I need recorded to a sheet in spreadsheet.
The only challenge that I need help is avoiding overwriting existing sheet data on subsequent loop, how can I handle this ?
This first part gets project codes and uses it to make API call where the teams on all the projects are contained in a response.
function readDates() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range1 = sheet.getRange("C2:C" + sheet.getLastRow()).getValues();
var searchString = "Project";
var ahead = nextweek()
var behind = lastweek()
var team_array = [];
for (var i = 0; i < range1.length; i++) {
if (range1[i][0] >= behind && range1[i][0] <= ahead) {
var lastRow = sheet.getRange(2 + i, 1, 1, 8).getValues();
var dateval = lastRow[0][2]
var data = {
'project_id': lastRow[0][3]
};
var options = {
method: 'get',
headers: {
Authorization: 'Bearer ' + AUTH
}
};
var url = tknurlL + Endpoint + data.project_id + '/users?auth=' + AUTH
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response);
var content = team.data;
team_array.push(content);
var ss = SpreadsheetApp.getActive().getSheetByName('Team');
const lr = sheet.getLastRow();
for (var j = 0; j < content.length; j++) {
ss.getRange(2 + j, 1).setValue(content[j].id);
ss.getRange(2 + j, 2).setValue(content[j].first_name);
ss.getRange(2 + j, 3).setValue(content[j].last_name);
ss.getRange(2 + j, 4).setValue(content[j].display_name);
ss.getRange(2 + j, 5).setValue(content[j].email);
ss.getRange(2 + j, 6).setValue(content[j].user_type_id);
ss.getRange(2 + j, 7).setValue(content[j].role);
}
}
}
}
回答1:
How about this modification?
Modification points:
- In your script, the same range of
getRange(2 + j, 1)is used as the start range every time in the loop. By this, the values are overwritten by newcontent. - In this modification, the for loop for
contentis removed and put the values at the outside of the for loop. The retrievedcontentis added to the array ofcontentsin the 1st for loop. By this, allcontentvalues (contents) can be put to the sheet ofTeambysetValues. And also, the process cost can be reduced a little.
Modified script:
When your script is modified, please modify as follows.
From:var team_array = [];
for (var i = 0; i < range1.length; i++) {
if (range1[i][0] >= behind && range1[i][0] <= ahead) {
var lastRow = sheet.getRange(2 + i, 1, 1, 8).getValues();
var dateval = lastRow[0][2]
var data = {'project_id': lastRow[0][3]};
var options = {method: 'get',headers: {Authorization: 'Bearer ' + AUTH}};
var url = tknurlL + Endpoint + data.project_id + '/users?auth=' + AUTH
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response);
var content = team.data;
team_array.push(content);
var ss = SpreadsheetApp.getActive().getSheetByName('Team');
const lr = sheet.getLastRow();
for (var j = 0; j < content.length; j++) {
ss.getRange(2 + j, 1).setValue(content[j].id);
ss.getRange(2 + j, 2).setValue(content[j].first_name);
ss.getRange(2 + j, 3).setValue(content[j].last_name);
ss.getRange(2 + j, 4).setValue(content[j].display_name);
ss.getRange(2 + j, 5).setValue(content[j].email);
ss.getRange(2 + j, 6).setValue(content[j].user_type_id);
ss.getRange(2 + j, 7).setValue(content[j].role);
}
}
}
To:
var contents = []; // Added
var team_array = [];
for (var i = 0; i < range1.length; i++) {
if (range1[i][0] >= behind && range1[i][0] <= ahead) {
var lastRow = sheet.getRange(2 + i, 1, 1, 8).getValues();
var dateval = lastRow[0][2]
var data = {'project_id': lastRow[0][3]};
var options = {method: 'get',headers: {Authorization: 'Bearer ' + AUTH}};
var url = tknurlL + Endpoint + data.project_id + '/users?auth=' + AUTH
var response = UrlFetchApp.fetch(url, options);
var team = JSON.parse(response);
var content = team.data;
team_array.push(content);
contents = contents.concat(content); // Added
}
}
// I added below script.
if (contents.length > 0) {
var dstSheet = SpreadsheetApp.getActive().getSheetByName('Team');
var values = contents.map(e => ([e.id, e.first_name, e.last_name, e.display_name, e.email, e.user_type_id, e.role]));
dstSheet.getRange(2, 1, values.length, values[0].length).setValues(values);
}
Reference:
- setValues(values)
来源:https://stackoverflow.com/questions/61656367/how-do-i-avoid-overwritting-a-row-in-google-sheet-while-seting-values-from-api-r