简体   繁体   English

如何在从 API 响应中设置值时避免覆盖谷歌表中的一行

[英]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?这个改装怎么样?

Modification points:修改点:

  • In your script, the same range of 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覆盖。
  • In this modification, the for loop for 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.而且,可以稍微降低加工成本。

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:参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何 Map API 响应 Google 表 - How do I Map API response to Google Sheet Firebase | 谷歌表 | 应用脚本 | 如何将列中的数据而不是行中的数据从 Firebase 保存到 Google 表格? - Firebase | Google Sheet | App Script | How do I save the data in column instead of row from Firebase to Google Sheet? 如何将Google Analytics(分析)API响应放入表格中? - How to put Google Analytics api response in a sheet? 如何遍历来自 API 的所有响应并使用 Javascript 将它们传递给 Google 表格? - How do I loop through all responses from API and pass them to Google Sheet using Javascript? 如何将其他谷歌表格中的值添加在一起并将它们显示在一张表格上? - How do i add values from other google sheets together and display them on one sheet? (Google Sheet API) 如何将 10 添加到 A1? (Javascript) - (Google Sheet API) How do I add 10 to A1? (Javascript) Google Drive Api v3如何使用Google表格格式上传? 400响应 - Google Drive Api v3 how can I upload with google sheet formatting? 400 response 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 如何使用 JavaScript/Google App 脚本在 Google 表格中连续映射 arrays - How do I get arrays mapped in a row in google sheet using JavaScript/Google App script 如何阻止 Google 工作表生成重复的表单? - How do I stop Google sheet from generating duplicating forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM