Google表格-脚本缓慢-依赖下拉

时间:2018-07-26 18:30:07

标签: google-apps-script google-sheets

我有三个相互依赖的下拉框。该代码可以正常工作,但实际上很慢,需要3秒才能更新下一个框。我已经习惯了VBA,这在Excel中没有问题,但实际上已在Google表格中显示。我在代码的第一行添加了一个Browser.msgBox("here");,从更改下拉列表到显示它需要2秒。这不正常吗?

代码如下:(列表选项存储在“命名范围”中,生成正确的名称并存储在下面的optName中)

function onEdit(event) {


  if (!event) return;

  //SETTINGS

  var ss = event.source;
  var sheet = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  var sheetName = sheet.getName()

  if(sheet.getName() == 'Damages & Repairs July 2018') {
    var lcol = 3; // leftmost column number you are monitoring
    var rcol =6; // rightmost column number you are monitoring
  }else{
    var lcol = 4; // leftmost column number you are monitoring
    var rcol =7; // rightmost column number you are monitorin
  }
  //


  if (sheetName == 'Refunds July 2018' || sheetName == 'Exchanges July 2018'|| sheetName == 'Damages & Repairs July 2018') {

    var sourceCol = r.getColumn();
    var sourceRow = r.getRow();

    var ss = SpreadsheetApp.getActiveSpreadsheet();

     if(sheetName == 'Damages & Repairs July 2018') {

       if(sourceCol == 4) {
         var optName = sheet.getRange(sourceRow, lcol ).getValue() + sheet.getRange(sourceRow, lcol + 1).getValue();
       } else if ( sourceCol == 5) {
         var optName = sheet.getRange(sourceRow, lcol + 1).getValue();
       } else {
         var optName = sheet.getRange(sourceRow, sourceCol).getValue();
       }
     } else {
       if(sourceCol == 5) {
         var optName = sheet.getRange(sourceRow, lcol ).getValue() + sheet.getRange(sourceRow, lcol + 1).getValue();
       } else if ( sourceCol == 6) {
         var optName = sheet.getRange(sourceRow, lcol + 1).getValue();
       } else {
         var optName = sheet.getRange(sourceRow, sourceCol).getValue();
       }
     }

    var optName = optName.replace(/\s/g, "");

    var tableRange = ss.getRangeByName(optName).getValues();
    var optList = "";

    if (sourceCol >= lcol && sourceCol <= rcol) {

   // define the dropdown/validation rules
   var rangeRule = SpreadsheetApp.newDataValidation().requireValueInList(tableRange);

   // Clear the validation
   //sheet.getRange(sourceRow, sourceCol + 1).clearDataValidations()
   //sheet.getRange(sourceRow, sourceCol + 1).clearContent()
   // set the dropdown validation for the row
   sheet.getRange(sourceRow, sourceCol + 1).setDataValidation(rangeRule); // set range to your range

    }

  } 
}

任何帮助将不胜感激。

谢谢 克里斯

更新

我将代码更改为此-但仍然没有那么快:

function onEdit(event) {


  //SETTINGS

  var ss = event.source;
  var sheet = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  var sheetName = sheet.getName();
  var sourceCol = r.getColumn();
  var sourceRow = r.getRow();


  if(sheet.getName() == 'Damages & Repairs July 2018') {
    var lcol = 3; // leftmost column number you are monitoring
    var rcol =6; // rightmost column number you are monitoring
  }else{
    var lcol = 4; // leftmost column number you are monitoring
    var rcol =7; // rightmost column number you are monitorin
  }

  var sourceRange = sheet.getRange('R' + sourceRow + 'C' + lcol + ':R' + sourceRow + 'C' + rcol).getValues();

  if (sheetName == 'Refunds July 2018' || sheetName == 'Exchanges July 2018'|| sheetName == 'Damages & Repairs July 2018') {

    var ss = SpreadsheetApp.getActiveSpreadsheet();

     if(sheetName == 'Damages & Repairs July 2018') {

       if(sourceCol == 4) {
         var optName = sourceRange[0][0] + sourceRange[0][1];
       } else if ( sourceCol == 5) {
         var optName = sourceRange[0][1];
       } else {
         var optName =sourceRange[0][0];
       }

     } else {

       if(sourceCol == 5) {
         var optName = sourceRange[0][0] + sourceRange[0][1];
       } else if ( sourceCol == 6) {
         var optName = sourceRange[0][1];
       } else {
         var optName = sourceRange[0][0];
       }

     }


    var optName = optName.replace(/\s/g, "");

    var tableRange = ss.getRangeByName(optName).getValues();
    var optList = "";

    if (sourceCol >= lcol && sourceCol <= rcol) {

   // define the dropdown/validation rules
   var rangeRule = SpreadsheetApp.newDataValidation().requireValueInList(tableRange);

   // Clear the validation
   sheet.getRange(sourceRow, sourceCol + 1).clearDataValidations()
   sheet.getRange(sourceRow, sourceCol + 1).clearContent()
   // set the dropdown validation for the row
   sheet.getRange(sourceRow, sourceCol + 1).setDataValidation(rangeRule); // set range to your range

    }

  } 
}

0 个答案:

没有答案