[英]How do I avoid overwritting a row in google sheet while seting values from API response
I have a JavaScript code that gets response from API and sets the values on google spreadsheet.我有一个 JavaScript 代码,它从 API 获得响应并在谷歌电子表格上设置值。
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.第一部分获取项目代码并使用它进行 API 调用,其中所有项目的团队都包含在响应中。
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);
}
}
}
}
How about this modification?这个改装怎么样?
getRange(2 + j, 1)
is used as the start range every time in the loop.getRange(2 + j, 1)
范围作为起始范围。 By this, the values are overwritten by new content
.content
覆盖。content
is removed and put the values at the outside of the for loop.content
的 for 循环被删除,并将值放在 for 循环的外部。 The retrieved content
is added to the array of contents
in the 1st for loop.content
被添加到第一个 for 循环中的contents
数组中。 By this, all content
values ( contents
) can be put to the sheet of Team
by setValues
.content
值( contents
)都可以通过setValues
放入Team
的工作表中。 And also, the process cost can be reduced a little. When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。
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); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.