0

使用应用程序脚本在谷歌电子表格中设置数据验证规则时遇到问题。下面是两个函数,一个是 onChange 函数,只要用户以任何方式更改电子表格,就会触发该函数。

每当从特定工作表中添加/删除一行时,都会触发 updatenamedataVal() 函数。

一切正常,代码执行。但是单元格中没有数据验证集!怎么了?

function myonChange(e){
  var ss = e.source.getActiveSheet();
  var ssname = ss.getSheetName();
  if((e.changeType === 'INSERT_COLUMN' || e.changeType === 'REMOVE_COLUMN') && ssname !== 'Aggregates'){
    Browser.msgBox('Whoops!','You are not allowed to add/remove columns. Please undo action by pressing CTRL+Z', Browser.Buttons.OK);  
  }
  else if((e.changeType === 'INSERT_ROW' || e.changeType === 'REMOVE_ROW') && ssname === 'Teacher Details'){
    var ssrange = ss.getDataRange();
    var valrange = ss.getRange(2,3,ssrange.getLastRow(),1);
    updatenamecelldataVal(valrange);
  } 
};


function updatenamecelldataVal(valrange){
      var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
      var numsheets = SpreadsheetApp.getActiveSpreadsheet().getNumSheets();
      var rule = SpreadsheetApp.newDataValidation().requireValueInRange(valrange).build();
      for(var i = 0; i < numsheets; i++){
        var sheetname = sheets[i].getName();
        var sheetrange = sheets[i].getDataRange();
        var sheetlastrow = sheetrange.getLastRow();
        if(sheetname !== 'Teacher Details' && sheetname !== 'Aggregates' && sheetname !== 'List of Subjects'){
          for(var j = 0; j < (sheetlastrow/17); j++){
            var namecell = sheetrange.getCell(4+(17*j), 2);
            namecell.clearDataValidations();
            namecell.setDataValidation(rule); 
          }
        }
      }
    }; 
4

2 回答 2

1

以下代码按预期工作,在第 4 行第 2 列中设置单元格中的数据验证。我更改了一些行来调整我的测试环境。

/*
In the Spreadsheet, create new trigger:
Run: onChange
Events: From spreadsheet
On change
*/

function onChange(e) {
  var ss = e.source, valrange;
  var s = ss.getActiveSheet();
  var sname = s.getSheetName();
  if((e.changeType === 'INSERT_COLUMN' || e.changeType === 'REMOVE_COLUMN') && sname !== 'Aggregates')
    Browser.msgBox('Whoops!','You are not allowed to add/remove columns. Please undo action by pressing CTRL+Z', Browser.Buttons.OK);  
  else if((e.changeType === 'INSERT_ROW' || e.changeType === 'REMOVE_ROW') && sname === 'Teacher Details') {
    valrange = s.getRange(2, 3, s.getDataRange().getLastRow() - 1, 1);
    updatenamecelldataVal(valrange, ss);
  }
}

function updatenamecelldataVal(valrange, ss) {
  var sheets = ss.getSheets(), namecell;
  var numsheets = sheets.length; //SpreadsheetApp.getActiveSpreadsheet().getNumSheets();
  var rule = SpreadsheetApp.newDataValidation().requireValueInRange(valrange).build();
  for(var i = 0; i < numsheets; i++) {
    var sheetname = sheets[i].getName();
    var sheetrange = sheets[i].getDataRange();
    var sheetlastrow = 17; //sheetrange.getLastRow();
    if(sheetname !== 'Teacher Details' && sheetname !== 'Aggregates' && sheetname !== 'List of Subjects') {
      for(var j = 0, len = sheetlastrow / 17; j < len; j++) {
        namecell = sheetrange.getCell(4 + (17 * j), 2);
        namecell.clearDataValidations();
        namecell.setDataValidation(rule); 
      }
    }
  }
}
于 2013-10-27T03:39:16.277 回答
0

这是我遇到问题的 wchiquito 脚本的更简单版本。它与提问者的代码不完全匹配,但应该更易于理解和适应:

function onChange(e) {

  var ss = e.source, valrange;
  var s = ss.getActiveSheet();
  var sname = s.getSheetName();

  if((e.changeType === 'INSERT_ROW' || e.changeType === 'REMOVE_ROW') && sname === 'Accounts') {
    valrange = s.getRange(2, 1, s.getMaxRows()-1, 1);
    updatenamecelldataVal(valrange, ss);
  }
}

function updatenamecelldataVal(valrange, ss) {
  var sheets = ss.getSheets();
  var targetCells;
  var rule = SpreadsheetApp.newDataValidation().requireValueInRange(valrange).build();
  for(var i = 0; i <= sheets.length; i++) {
    var sheetname = sheets[i].getName();
    if (sheetname !== 'Accounts') {
      targetCells = sheets[i].getRange(2, 1, sheets[i].getMaxRows(), 1);
      targetCells.clearDataValidations();
      targetCells.setDataValidation(rule);
    }
  }
}
于 2014-09-04T08:15:39.643 回答