首页 > 解决方案 > 谷歌脚本超时

问题描述

多年来,我一直在 Excel 中使用宏,并希望将其翻译成谷歌脚本以在云端硬盘中进行协作。

我正在使用两张表设置(一张名为“BILAN”,这是概述,另一张名为 INPUT 用于输入数据。该脚本在没有太多输入的情况下工作得很好,但我希望达到近一千个输入在文件使用结束时。

基本上,脚本是一个双循环来总结 BILAN 表中的输入。在此先感谢您的帮助 !!

这是我正在使用的代码:

function getTransportDates() {
  var ss = SpreadsheetApp.getActive();

 var strDatesTransport = '';
 const intNbClients = ss.getSheetByName('BILAN').getDataRange().getLastRow();
 const intNbInputs = ss.getSheetByName('INPUT').getDataRange().getLastRow();

 for (let i = 4; i <= intNbClients; i++) {    // loop through the addresses in BILAN
  if (ss.getSheetByName('BILAN').getRange(i, 9).getValue() >0) {
   for (let j = 4; j <= intNbInputs; j++) {  // loop through the adresses in INPUT
    if (ss.getSheetByName('INPUT').getRange(j, 2).getValue() == ss.getSheetByName('BILAN').getRange(i, 1).getValue()) {
     strDatesTransport = strDatesTransport + ' // ' + ss.getSheetByName('INPUT').getRange(j, 1).getValue(); //.toISOString().split('T')[0];
    }
   }
  }
  ss.getSheetByName('BILAN').getRange(i, 10).setValue(strDatesTransport);
  strDatesTransport = '';
 }
};

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


试试这种方式:

function getTransportDates() {
  const ss = SpreadsheetApp.getActive();
  var sdt = '';
  const csh = ss.getSheetByName('BILAN');
  const cvs = csh.getRange(4, 1, csh.getLastRow() - 3, csh.getLastColumn()).getValues();
  const ish = ss.getSheetByName('INPUT');
  const ivs = ish.getRange(4, 1, ish.getLastRow() - 3, ish.getLastColumn()).getValues();
  cvs.forEach((cr,i) => {
    if((cr[8] > 0)) {
      ivs.forEach((ir,j)=>{
        if(ir[1] == cr[0]) {
          sdt += ir[0];
        }
      });
    }
    ss.getSheetByName('BILAN').getRange(i + 4, 10).setValue(sdt);
    sdt = '';
  });
}

不知道这是哪里://.toISOString().split('T')[0];


推荐阅读