首页 > 解决方案 > 如何优化此 Google Apps 脚本?它所做的只是读取和写入数据,但它需要永远

问题描述

我有(我认为是)一个简单的 Google Apps 脚本,它从 URL 中获取参数,并根据一行中的两个单元格是否与两个参数匹配,使用它们将数据写入电子表格:

function doGet(e) {  
  var email = e.parameter.email;
  var originalTitle = e.parameter.originalTitle;
  var newTitle = e.parameter.newTitle;
  var newAdditionalAuthors = e.parameter.newAdditionalAuthors;
  var newTechnicalSessions = e.parameter.newTechnicalSessions;
  var newApproval = e.parameter.newApproval;

  var sh = SpreadsheetApp.openById("18vghxzzt27FcBOvoG8kIFFtswMB7XTsMRg60DJ6_-QM").getSheetByName("Responses");
  var data = sh.getDataRange().getValues(); // read all data in the sheet

  for (n=0;n<data.length;++n){ 
    if (data[n][2] == email && data[n][3] == originalTitle){ //Find all rows where the email matches the submitter's and the title matches the original
      data[n][3] = newTitle;
      data[n][31] = newAdditionalAuthors;
      data[n][33] = newTechnicalSessions;
      data[n][34] = newApproval;
    }; 
    sh.getRange(1,1,data.length,data[0].length).setValues(data); // write back to the sheet
  }

  return ContentService.createTextOutput("Success. You may close this tab.");
}

但是,脚本运行似乎需要相当多的时间,而且工作表中的条目越多,情况就越糟。有人可以告诉我我做错了什么吗?

标签: javascriptgoogle-apps-script

解决方案


推荐阅读