【问题标题】:Google apps script too slow谷歌应用脚​​本太慢
【发布时间】:2021-12-09 18:48:27
【问题描述】:

所以我的脚本需要 5 分钟才能运行。 它将文件复制并粘贴到文件夹中,然后用 {{}} 替换单词处的文本。

function b(row,sheet){
  return sheet.getRange(row,sheet.getMaxColumns()).getValue()
}
function makedoc(){
  let forma = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("forma");
  let datab = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("baza");
  let rows = forma.getRange(1,2,forma.getMaxRows()).getValues();
  let googleDocTemplate = DriveApp.getFileById("notrevealing");
  let destinationFolder = DriveApp.getFolderById("not revealing");
  let copy = googleDocTemplate.makeCopy(rows[1]+" "+rows[2]+" File", destinationFolder);
  let doc = DocumentApp.openById(copy.getId());
  let body = doc.getBody();
  body.replaceText("{{date1}}", Utilities.formatDate(new Date(), "GMT+2", "dd.MM/yyyy"));
  body.replaceText("{{date2}}", Utilities.formatDate(new Date(), "GMT+2", "dd.MM.yyyy"));
  body.replaceText("{{date3}}", Utilities.formatDate(new Date(), "GMT+2", " dd.MM "));
  body.replaceText("{{name}}",b(2,forma));
  body.replaceText("{{name2}}",b(3,forma));
  body.replaceText("{{name3}}",b(4,forma));
  body.replaceText("{{firma}}",b(25,forma));
  body.replaceText("{{price}}",b(23,forma));
  body.replaceText("{{price2}}",word(b(23,forma)));
  body.replaceText("{{price3}}",String(b(23,forma)+" dl."));
  body.replaceText("{{price4}}",String(word(b(23,forma))+" dl."));
  body.replaceText("{{a2}}",b(9,forma));
  body.replaceText("{{a3}}",b(10,forma));
  body.replaceText("{{a4}}",b(11,forma));
  body.replaceText("{{a5}}",b(12,forma));
  body.replaceText("{{a6}}",b(13,forma));
  body.replaceText("{{a7}}",b(14,forma));
  body.replaceText("{{a8}}",b(15,forma));
  body.replaceText("{{a9}}",b(15,forma));
  body.replaceText("{{a10}}",b(16,forma));
  body.replaceText("{{a11}}",b(17,forma));
  body.replaceText("{{a12}}",b(18,forma)); 
  body.replaceText("{{egn}}",b(7,forma));
  body.replaceText("{{number}}",b(5,forma));
  body.replaceText("{{address}}",b(6,forma));
  body.replaceText("discount",b(24,forma));
  body.replaceText("pay",b(23,forma)-b(24,forma));
  body.replaceText("discount2",word(b(24,forma)));
  body.replaceText("pay2",word(b(23,forma)-b(24,forma)));
}

如果你问我为什么要自己做每一件事,我试过用 for 但没用。

【问题讨论】:

标签: javascript google-apps-script google-sheets google-docs


【解决方案1】:

仍然存在问题,因为 word 是未定义的,但是这会运行得更快,因为它一次获取所有替换数据。

function makedoc() {
  const c = getMaxColumns();
  const ss = SpreadsheetApp.getActive();
  let fsh = ss.getSheetByName("forma");
  let dsh = ss.getSheetByName("baza");
  let fvs = fsh.getRange(2, 2, fsh.getMaxRows() - 1, fsh.getLastColumn()).getValues();
  let gtemp = DriveApp.getFileById("notrevealing");
  let dfldr = DriveApp.getFolderById("not revealing");
  let copy = gtemp.makeCopy(fvs[1] + " " + fvs[2] + " File", dfldr);
  let doc = DocumentApp.openById(copy.getId());
  let body = doc.getBody();
  body.replaceText("{{date1}}", Utilities.formatDate(new Date(), "GMT+2", "dd.MM/yyyy"));
  body.replaceText("{{date2}}", Utilities.formatDate(new Date(), "GMT+2", "dd.MM.yyyy"));
  body.replaceText("{{date3}}", Utilities.formatDate(new Date(), "GMT+2", " dd.MM "));
  body.replaceText("{{name}}",fvs[2-2][c-1]);
  body.replaceText("{{name2}}",fvs[3-2][c-1]);
  body.replaceText("{{name3}}",fvs[4-2][c-1]);
  body.replaceText("{{firma}}",fvs[25-2][c-1]);
  body.replaceText("{{price}}",fvs[23-2][c-1]);
  body.replaceText("{{price2}}",word(fvs[23-2][c-1]));
  body.replaceText("{{price3}}",String(fvs[23-2][c-1])+" dl.");
  body.replaceText("{{price4}}",String(word(fvs[23-2][c-1]))+" dl.");
  body.replaceText("{{a2}}",fvs[9-2][c-1]);
  body.replaceText("{{a3}}",fvs[10-2][c-1]);
  body.replaceText("{{a4}}",fvs[11-2][c-1]);
  body.replaceText("{{a5}}",fvs[12-2][c-1]);
  body.replaceText("{{a6}}",fvs[13-2][c-1]);
  body.replaceText("{{a7}}",fvs[14-2][c-1]);
  body.replaceText("{{a8}}",fvs[15-2][c-1]);
  body.replaceText("{{a9}}",fvs[15-2][c-1]);
  body.replaceText("{{a10}}",fvs[16-2][c-1]);
  body.replaceText("{{a11}}",fvs[17-2][c-1]);
  body.replaceText("{{a12}}",fvs[18-2][c-1]); 
  body.replaceText("{{egn}}",fvs[7-2][c-1]);
  body.replaceText("{{number}}",fvs[5-2][c-1]);
  body.replaceText("{{address}}",fvs[6-2][c-1]);
  body.replaceText("discount",fvs[24-2][c-1]);
  body.replaceText("pay",fvs[23-2][c-1]-fvs[24-2][c-1]);
  body.replaceText("discount2",word(fvs[24-2][c-1]));
  body.replaceText("pay2",word(fvs[23-2][c-1]-fvs[24-2][c-1]));
}

它很可能仍然需要调整,因为我使用正则表达式进行了所有替换,但没有我不希望生成的数据,因此很难进一步调试,并且根据您以前的情况,您可能不知道如何接受它你自己走得更远。所以请提供数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多