简体   繁体   English

如何优化 Google Spreadsheet function 执行时间?

[英]How to optimize Google Spreadsheet function execution time?

function findAgentRow(){
  let row = 2;
  let emailCell;
  let maxRow = sheet.getLastRow();
  while(--maxRow){
    emailCell = sheet.getRange(row, findColumn(sheet, "Agent"));
    if(emailCell.getValue() == agentEmail){
      return row
    }else if(row == sheet.getLastRow()){
       return 0;
    }else{  
        row = row + 1;
    }
  }
}

I have written a google spreadsheets function that locates a row containing information about a person.我写了一个谷歌电子表格 function,它找到了包含一个人信息的一行。 It checks whether the row contains the person's email. The issue is that when the spreadsheet contains more than 700 rows the execution time is more than 2 minutes.它检查该行是否包含此人的 email。问题是当电子表格包含超过 700 行时,执行时间超过 2 分钟。

Is there a way to optimize it?有没有办法优化它?

Try not to use findColumn in each loop.尽量不要在每个循环中使用findColumn

You should also aim at using only one getValues .您还应该致力于只使用一个getValues

function findAgentRow(agentEmail) {
  // const sheet = SpreadsheetApp.getActiveSheet();
  const row = 2;
  const col = findColumn(sheet, 'Agent');
  const values = sheet.getRange(row, col, sheet.getLastRow() - 1, 1).getValues().flat();
  return values.indexOf(agentEmail) + row;
}

function findColumn(sheet, value) {
  const row = 1;
  const values = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
  return values.indexOf(value) + 1;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM