无论如何要加快以下代码在谷歌表脚本?

时间:2018-07-30 01:46:47

标签: google-apps-script google-sheets

我对使用Google脚本进行编码非常陌生,因此您可能会看下面的代码,并因其目前的效率低下而摇头。简而言之,这段代码正在检查单元格A5至D5的值,并查看其在主列表中是否具有与E5和F5相同的值。如果不是,则将A5-D5复制到另一个工作簿上的两个不同列表中。该代码目前有效,但我想知道是否还有任何方法可以加快速度?它主要由Ipad上的IOS工作表应用触发(因此为什么我必须使用onEdit而不是按钮来触发)。当检查它是否已经在列表中时,它在同一工作簿中的本地工作表上,该工作表通过外部工作簿中的公式进行更新。您能提供的任何帮助将不胜感激。就像我说的,尽管我是编码的新手,但是当我遇到问题时,我还是通过Google的帮助将其拼凑在一起。

function onEdit(e) {
var sheet1 = SpreadsheetApp.getActive().getSheetByName("sheet1")
var SiteName = sheet1.getRange("A5").getValue()
var Type = sheet1.getRange("B5").getValue()
var Fleet = sheet1.getRange("C5").getValue()
var Rego = sheet1.getRange("D5").getValue()
var Inside = sheet1.getRange("E5").getValue()
var Outside = sheet1.getRange("F5").getValue()
var User = sheet1.getRange("A3").getValue()
var master = SpreadsheetApp.getActive().getSheetByName("Master Wash List");
var True = true
var False = false
var mastermaster = SpreadsheetApp.openById("links to external workbook2").getSheetByName("master")
var mastertolocal = SpreadsheetApp.openById("links to external workbook2").getSheetByName("MasterToLocal")
var sheet = SpreadsheetApp.getActive().getSheetByName("Master Wash List");
var lastrow = sheet.getLastRow()
var SubmitButton = SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").getValue()
if(Inside == True) {
for(var x = 1; x < lastrow+1;x++) {
  var cell = master.getRange(x,3).getValue()
  var masterinside = master.getRange(x, 4).getValue()
  var masteruser = master.getRange(x,6).getValue()
  if(Fleet == cell && masterinside != "N/A"){
    SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
    SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([cell + " has already been Internally washed on " + masterinside + " by " + masteruser])
    SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
    }
}
}
if(Outside == True) {
for(var y = 2; y < lastrow+1;y++) {
  var cell = master.getRange(y,3).getValue()
  var masteroutside = master.getRange(y, 5).getValue()
  var User1 = master.getRange(y, 6).getValue()
  if(Fleet == cell && masteroutside != "N/A"){
    SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
    SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([cell + " has already been externally washed on " + masteroutside + " by " + User1])
    SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17,2)
    SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")}
}
}
if(SubmitButton == True){
if(Inside == True && Outside == True && Fleet != "" ) {
      mastermaster.appendRow([SiteName, Type, Fleet, Rego, new Date(), new Date(), User,])
      mastertolocal.appendRow([SiteName, Type, Fleet, Rego, "N/A", new Date(), User])
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("E5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Internally & Externally."])
      SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)}
else if(Outside == True && Fleet != "" ) {
      mastermaster.appendRow([SiteName, Type, Fleet, Rego,"N/A", new Date(), User])
      mastertolocal.appendRow([SiteName, Type, Fleet, Rego, "N/A", new Date(), User])
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Externally."])
      SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2) }
else if(Inside == True && Fleet != "") {
      mastermaster.appendRow([SiteName, Type, Fleet, Rego, new Date(), "N/A", User])
      mastertolocal.appendRow([SiteName, Type, Fleet, Rego, new Date(), "N/A", User])
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("E5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("G5").setValue("false")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("A5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("B5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("C5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("D5").setValue("")
      SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue([Fleet + " has successfully been Washed Internally."])
      SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)}
else if(Inside != True && Outside != True && Fleet != "") {
      SpreadsheetApp.getActive().getSheetByName("sheet1").insertRowAfter(6)
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange(7,1).setValue(["Please select if " + Fleet + " Has been washed Internally, Externally or Both"])
      SpreadsheetApp.getActive().getSheetByName("sheet1").deleteRows(17, 2)  
      SpreadsheetApp.getActive().getSheetByName("sheet1").getRange("F5").setValue("false")}
  }
}

1 个答案:

答案 0 :(得分:1)

Google提供了Best Practices资源,您一定要查看。从那里开始,最相关的部分是关于使用批处理操作。并非特定于Google Apps脚本,而是一种好的做法,是减少代码中的某些重复(请参见DRY principle)。以下是一些您可以应用于整个脚本的特定说明:

  1. 通过将返回值放入变量来减少调用SpreadsheetApp.getActive()SpreadsheeApp.openById()的次数。

    var ss = SpreadsheetApp.getActive();
    var externalSS = SpreadsheetApp.openById("links to external workbook2");
    
  2. 通过使用已经创建的变量SpreadsheetApp.getActive().getSheetByName("sheet1")减少调用sheet1的次数。您可以使用查找并全部替换来解决此问题。

  3. 通过改用getValues()并访问数组值来减少调用getValue()的次数。

    var row5 = sheet1.getRange(1, 1, 1, 7).getValues(); // A5:G5
    var SiteName = row5[0][0]; // A5
    var Type = row5[0][1]; // B5
    var Fleet = row5[0][2]; // C5
    var Rego = row5[0][3]; // D5
    var Inside = row5[0][4]; // E5
    var Outside = row5[0][5]; // F5
    
  4. truefalse是javascript中的常量,因此无需将它们放入变量TrueFalse中。
  5. new Date()放在变量中可以减少调用次数;

    var now = new Date();
    
  6. Similary指向第3点,通过改为使用setValues()来减少致电setValue()的次数。

    row5.setValues([ // row5 previously defined A5:G5
      [""], // A5
      [""], // B5
      [""], // C5
      [""], // D5
      [false], // E5
      [false], // F5
      [false] //G5
    ]);
    

我上面编写的示例只是示例,但它们基于您的代码。您需要仔细阅读整个脚本,并弄清楚如何正确应用这些原理,而又不会破坏脚本的功能。