使脚本更快/更高效

时间:2017-09-26 19:36:30

标签: google-apps-script

我正在尝试使用脚本检查sheet1作为名字和姓氏(2个单元格),使用sheet1中每个人的名字和姓氏,然后在sheet2中检查每个人的值是否大于4-5行0

问题是某些名称在不同的列中,我需要检查一列,并在一个列中检查这些名称与大(180个单元格)名称列表。这是它超时的部分。

我的代码功能齐全且工作正常,但它过于频繁地使用某些部分并最终超时。 getvalue函数被大量使用,我不确定使用它的更好方法。我正在寻找如何使其更有效率或更好地运行。

我可能只需要在代码中输入名称以使其更快,但我希望有一种更加用户友好的方法,以便在脚本中自动更改sheet1和格式的名称。

谢谢!

function getvalue(CellName) {
  return SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet2').getRange(CellName).getValues();
}

function getAdvisee(CellName) {
 return SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1').getRange(CellName).getValues(); 
}

function teacher1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
  var LastRow = ss.getLastRow();
  var columns = 3;
  var columnassignments = 3;
  var columnEnrichment = 3;
  var columnAmount = 3;
  var advisoryStudent = getvalue('a' + columnEnrichment);
  var studentEnrichment = getvalue('g'+ columnEnrichment);
  var hours = Utilities.formatDate(studentEnrichment, 'EST', 'mm');
  var minutes = Utilities.formatDate(studentEnrichment, 'EST', 'ss');
  var teacherEmail = getAdvisee('b2');
  var student1 = getAdvisee('b3');
  var student1F = getAdvisee('b3');
  var student2 = getAdvisee('b4');
  var student2F = getAdvisee('b4');
  var student3 = getAdvisee('b5');
  var student3F = getAdvisee('b5');
  var student4 = getAdvisee('b6');
  var student4F = getAdvisee('b6');
  var student5 = getAdvisee('b7');
  var student5F = getAdvisee('b7');
  var student6 = getAdvisee('b8');
  var student6F = getAdvisee('b8');
  var student7 = getAdvisee('b9');
  var student7F = getAdvisee('c9');
  var student8 = getAdvisee('b10');
  var student8F = getAdvisee('c10');
  var student9 = getAdvisee('b11');
  var student9F = getAdvisee('c11');
  var student10 = getAdvisee('b12');
  var student10F = getAdvisee('c12');
  var student11 = getAdvisee('b13');
  var student11F = getAdvisee('c13');


  for (columns; columns <= LastRow && columns <= 160; columns++) {
    var advisoryStudent = getvalue('a' + columns);
    var advisoryStudentSp = getvalue('b' + columns);
    var reminderCount = getvalue('e' + columns);
    var reminderLink = getvalue('m'+columns);
    if (advisoryStudentSp == student1F || advisoryStudent == student2 && advisoryStudentSp == student2F || advisoryStudentSp == student3F || advisoryStudentSp == student4F || advisoryStudentSp == student5F || advisoryStudentSp == student6F || advisoryStudentSp == student7F && advisoryStudent == student7 || advisoryStudentSp == student8F || advisoryStudent == student9 && advisoryStudentSp == student9F|| advisoryStudent == student10 && advisoryStudentSp == student10F || advisoryStudent == student11 && advisoryStudentSp == student11F) {
      if ( getvalue('e' + columns) > 0 ) {
        reminders = reminders + getvalue('b' + columns) + ' ' + getvalue('a' + columns) + ': ' + reminderCount + '<br>Reminder Link: ' + reminderLink + '<br>';
      }
    }
  }

  for (columnassignments; columnassignments <= LastRow && columnassignments <= 160; columnassignments++) {
    var advisoryStudent = getvalue('a' + columnassignments);
    var advisoryStudentSp = getvalue('b'+columnassignments);
    var assignmentCount = getvalue ('f' + columnassignments);
    var assignmentLink = getvalue('n' + columnassignments);
    if (advisoryStudentSp == student1F || advisoryStudent == student2 && advisoryStudentSp == student2F || advisoryStudentSp == student3F || advisoryStudentSp == student4F || advisoryStudentSp == student5F || advisoryStudentSp == student6F || advisoryStudentSp == student7F && advisoryStudent == student7 || advisoryStudentSp == student8F || advisoryStudent == student9 && advisoryStudentSp == student9F|| advisoryStudent == student10 && advisoryStudentSp == student10F || advisoryStudent == student11 && advisoryStudentSp == student11F) {
      if (getvalue('f' + columnassignments) > 0) {
        assignments = assignments + getvalue('b' + columnassignments) + ' ' + getvalue('a' + columnassignments) + ': ' + assignmentCount + '<br>Assignment Link: ' + assignmentLink + '<br>';
      }
    }
  }

    for (columnAmount; columnAmount <= LastRow && columnAmount <= 160; columnAmount++) {
      var advisoryStudent = getvalue('a' + columnAmount);
      var advisoryStudentSp = getvalue('b' + columnAmount);
      var amountowed = getvalue ('i' + columnAmount);
      if (advisoryStudentSp == student1F || advisoryStudent == student2 && advisoryStudentSp == student2F || advisoryStudentSp == student3F || advisoryStudentSp == student4F || advisoryStudentSp == student5F || advisoryStudentSp == student6F || advisoryStudentSp == student7F && advisoryStudent == student7 || advisoryStudentSp == student8F || advisoryStudent == student9|| advisoryStudent == student10 && advisoryStudentSp == student10F || advisoryStudent == student11 && advisoryStudentSp == student11F) {
        if (getvalue('i' + columnAmount) > 0) {
          amount = amount + getvalue('b' + columnAmount) + ' ' + getvalue('a' + columnAmount) + ': ' + '$' + amountowed + '<br>';
        }
      }
    }
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Status Calculator');
  var LastRow = ss.getLastRow();
  var columns = 3;
  var columnassignments = 3;
  var columnEnrichment = 3;
  for (columnEnrichment; columnEnrichment <= LastRow && columnEnrichment <= 160; columnEnrichment++) {
    var advisoryStudent = getvalue('a'+columnEnrichment);
    var advisoryStudentSp = getvalue('b' + columnEnrichment);
    if (advisoryStudentSp == student1F || advisoryStudent == student2 && advisoryStudentSp == student2F || advisoryStudentSp == student3F || advisoryStudentSp == student4F || advisoryStudentSp == student5F || advisoryStudentSp == student6F || advisoryStudentSp == student7F && advisoryStudent == student7 || advisoryStudentSp == student8F || advisoryStudent == student9 && advisoryStudentSp == student9F|| advisoryStudent == student10 && advisoryStudentSp == student10F || advisoryStudent == student11 && advisoryStudentSp == student11F) {
      if (Utilities.formatDate(getvalue('g' + columnEnrichment), 'EST', 'HH') > 0 || Utilities.formatDate(getvalue('g' + columnEnrichment), 'EST', 'mm') > 0){
        enrichment = enrichment + getvalue('b' + columnEnrichment) + ' ' + getvalue('a' + columnEnrichment) + ': ' + Utilities.formatDate(getvalue('g' + columnEnrichment), 'EST', 'HH') + ' hours ' + Utilities.formatDate(getvalue('g' + columnEnrichment), 'EST', 'mm') + ' minutes <br>'
      } 
    }
  }

}

编辑:

此部分只有一列中的名称列表,并在整个200行列中搜索一个单词并返回包含该单词的行,如果大于5,则再次返回。

如果我只是查找大于或等于5的数字并使用getvalue函数,那么这是返回值为5或更大的名称的最有效方法吗?

function getValues(CellName) {
 return SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2').getRange(CellName).getValues(); 
}

function setvalue(CellName, value) {
 return SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet2').getRange(CellName).setValue(value); 
}

function test(i) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh2 = ss.getSheetByName('Sheet2');
  var data = sh2.getRange('A1:C200').getValues();
  var amount = sh2.getRange('c1:c200').getValues()
  var findthis = sh2.getRange('e1').getValues();

  var acol = sh2.getRange(1,sh2.getLastRow(),1,1).getValues();
  var bcol = sh2.getRange(1,sh2.getLastRow(), 2, 1).getValues();

  var test = acol.join().split(',');
  var test2 = bcol.join().split(',');

  for (i = 0; i<data.length; i++) { //search all rows
      if ('wet' == data[i][0]) { //finds wet in the data column and returns row
       Logger.log('testing' + ' row '  + (i+1)) 

       if (amount[i][0] >= 5) { //search if c >= 5
         Logger.log('Larger ' + ' row '  + (i+1)) 
       } 

      }

    }

}

2 个答案:

答案 0 :(得分:2)

不是使用getValue()/ getAdvisee()公式逐个检索单元格值,而是需要使用Range.getValues()一次读取整个工作表或相关范围,然后使用阵列。

这里的关键问题是每次调用Range.getValues()都会消耗大量时间,无论是一次读入100个单元格还是单个单元格值。因此,您目前在一个单元格中一次读取的方法非常慢(正如您所看到的那样)。

以这种方式重构太大而不能作为我答案的一部分,但一旦完成,这个脚本将在几秒钟内运行。

答案 1 :(得分:2)

我建议将数据按其自然网格范围分组到arrays,然后按数组索引访问值。

以下内容:

function teacher1() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh_1 = ss.getSheetByName('sheet1');
  var sh_2 = ss.getSheetByName('sheet2');
  ...
   // Get all related values into array & retrieve each row record by index
   var student = sh_1.getRange("B3:C13").getValues(); // student[row][col] :ex: student11 => student[12][0]::student11F => student[12][1]

  ...
  // remove the for loop & get all values in a col with one call per col
  //(this could be optimized further)
  var a_col = sh_2.getRange(1, sh_2.getLastRow(), 1, 1).getValues(); // col a rows 1-lastrow()
  var b_col = sh_2.getRange(1, sh_2.getLastRow(), 2, 1).getValues(); // col b rows 1-lastrow()

  // Flatten the array to make searching that column easier
  var advisoryStudent = a_col.join().split(",")
  var advisoryStudentSp = b_col.join().split(",")
  // etc ...


  // To check if student11 is in advisoryStudentSp: (can loop through students here)
  // -1 means not found
  // any other number is the index position of `advisoryStudentSp[i]`
  // if 5 is returned then student11 matched the value in sheet2 colB row6
  Logger.log("student11 is in position: %s", advisoryStudentSp.indexOf(student[12][0]))

}

编辑:添加了一些重要的.getValues()