【问题标题】:How to optimize Google Spreadsheet function execution time?如何优化谷歌电子表格函数的执行时间?
【发布时间】:2021-10-23 09:54:40
【问题描述】:
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;
    }
  }
}

我编写了一个 google 电子表格函数,该函数可以定位包含某人信息的行。它检查该行是否包含此人的电子邮件。问题是当电子表格包含超过 700 行时,执行时间超过 2 分钟。

有办法优化吗?

【问题讨论】:

标签: javascript loops google-apps-script google-sheets optimization


【解决方案1】:

尽量不要在每个循环中使用findColumn

您还应该瞄准只使用一个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;
}

【讨论】:

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