简体   繁体   English

使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢

[英]Slow Execution for Copying Data from One Sheet to Another Using Google App Script

I would like your help in speeding up this script please.我想请您帮助加快此脚本的速度。 I got the script from an answer from Stack overflow.我从堆栈溢出的答案中得到了脚本。 It is taking longer than it should (30-50 seconds), although previously it didn't.它花费的时间比它应该的要长(30-50 秒),尽管以前没有。

function FormExec() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sinput = ss.getSheetByName("Slip");

  var rng1 = sinput.getRange("B3").getValue();
  var rng2 = sinput.getRange("D3").getValue();
  var rng3 = sinput.getRange("B5").getValue();
  var rng4 = sinput.getRange("D5").getValue();
  var rng5 = sinput.getRange("B7").getValue();
  var rng6 = sinput.getRange("D7").getValue();
  var rng7 = sinput.getRange("J1").getValue();

  if(rng1 == 0 || rng2 == 0 || rng3 == 0 || rng4 == 0 || rng5 == 0 || rng6 == 0) {

  Browser.msgBox('Please fill all required fields.');

  } else {

  var soutput = ss.getSheetByName("Form Responses 5");
  var input = sinput.getRange(1, 10, 15).getValues();
  var flatin = [].concat.apply([], input);
  soutput.getRange(soutput.getLastRow()+1, 1,1,15).setValues([flatin]);
  soutput.insertRowAfter(soutput.getLastRow());  
    resetSlip();
  };
}

function resetSlip() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sinput = ss.getSheetByName("Slip");
  sinput.getRange("B3").clearContent();
  sinput.getRange("B11:H11").clearContent();
  sinput.getRange("B19").clearContent();
  sinput.getRange("D3").clearContent();
  sinput.getRange("B21:E21").clearContent();
  sinput.getRange("B5").clearContent();
  sinput.getRange("B15:E15").clearContent();
  sinput.getRange("B13:I13").clearContent();
  sinput.getRange("D5").clearContent();
  sinput.getRange("B9:I9").clearContent();
  sinput.getRange("B7").clearContent();
  sinput.getRange("D7").clearContent();
  sinput.getRange("B17:C17").clearContent();
  sinput.getRange("B5").setFormula("=K1");
  sinput.getRange("D3").setValue("=L1");
  sinput.getRange("B7").setValue("=M1");
}

This is probably the best your going to get.这可能是你得到的最好的结果。 Personally, I wouldn't mess around with Form Response Pages.就个人而言,我不会乱用表单响应页面。 I'd recommend using an onFormSubmit trigger and building a page of your own.我建议使用 onFormSubmit 触发器并构建您自己的页面。

function FormExec() {
  var ss = SpreadsheetApp.getActive();
  var sh=ss.getSheetByName("Slip");
  var ui=SpreadsheetApp.getUi();

  var rng1 = sh.getRange("B3").getValue();
  var rng2 = sh.getRange("D3").getValue();
  var rng3 = sh.getRange("B5").getValue();
  var rng4 = sh.getRange("D5").getValue();
  var rng5 = sh.getRange("B7").getValue();
  var rng6 = sh.getRange("D7").getValue();
  var rng7 = sh.getRange("J1").getValue();

  if(rng1==0||rng2==0||rng3==0||rng4==0||rng5==0||rng6==0) {
    ui.alert('Please fill all required fields.');
  }else{
    var soutput=ss.getSheetByName("Form Responses 5");
    var input=sh.getRange(1,10,15,1).getValues();
    var flatin = input.map(function(r){return r[0];});
    soutput.getRange(soutput.getLastRow()+1, 1,1,flatin.length).setValues([flatin]);
    soutput.insertRowAfter(soutput.getLastRow());  
    sh.getRange("B3").clearContent();
    sh.getRange("B11:H11").clearContent();
    sh.getRange("B19").clearContent();
    sh.getRange("D3").clearContent();
    sh.getRange("B21:E21").clearContent();
    sh.getRange("B5").clearContent();
    sh.getRange("B15:E15").clearContent();
    sh.getRange("B13:I13").clearContent();
    sh.getRange("D5").clearContent();
    sh.getRange("B9:I9").clearContent();
    sh.getRange("B7").clearContent();
    sh.getRange("D7").clearContent();
    sh.getRange("B17:C17").clearContent();
    sh.getRange("B5").setFormula("=K1");
    sh.getRange("D3").setFormula("=L1");
    sh.getRange("B7").setFormula("=M1");
  }
}

Scripts that affect the spreadsheet looks tend to be slow.影响电子表格的脚本看起来很慢。 I'd reorganize my date to make it possible to get all your data and one time in an array and process it all at one time and replace it with a minimum number of setValues() and then a SpreadsheetApp.flush().我会重新组织我的日期,以便可以在一个数组中一次获取所有数据并一次处理所有数据,然后用最少数量的 setValues() 替换它,然后用一个 SpreadsheetApp.flush() 替换它。

暂无
暂无

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

相关问题 Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 如何使用 Google App Script 将一个 Google Sheet 中的所有数据提取到另一个 Google Sheet Tab? - How do I pull All data in one Google Sheet to another Google Sheet Tab using Google App Script? Google Apps脚本在一个工作表中复制一列,将其转置,然后粘贴到另一个 - Google Apps Script Copying a column in one sheet, transposing it, then pasting to another 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 使用Java Script将数据从一个文本框复制到另一个文本框 - Copying data from one text box to another using Java Script 使用单个脚本将数据从一张纸传输到另一张纸 - Transferring data from one sheet to another using a single script Google脚本将单元格的一部分复制到另一个工作表 - Google Script Copying part of a cell to another sheet Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 将一张工作表中的原始数据排序到Google App脚本中新工作表中的单独标签中 - Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script 使用Google脚本将数据从一个Google电子表格拉到另一电子表格 - Pull data from one Google spreadsheet to another using Google script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM