表格在一系列单元格(假到真)的编辑上运行脚本

时间:2018-09-05 17:08:38

标签: google-apps-script google-sheets

我有一个脚本,在编辑工作表时需要运行该脚本。每当选中L列(L2:L260)中的框时,我都需要它运行。我最初编写的脚本是在按钮按下时运行的(选中多个复选框,单击按钮即可运行),但其他需要使用它的人无法运行该脚本。最初,单击按钮时脚本运行良好,但是一旦我添加了onEdit内容,它便完全停止工作。这是我所拥有的:

function onEdit(e) {
  //Get the sheet you want to work with. 
  var editrange = {
    top : 2,
    bottom : 260,
    left : 11,
    right : 11};
  var thisrow = e.range.getrow();
  if (thisrow < editrange.top || thisrow > editrange.bottom)
    return;
  var thiscolumn = e.range.getcolumn();
  if (thiscolumn < editrange.left || thiscolumn > editrange.right)
    return;
  var ss = e.range.getSheet();
  var sheet = ss.getSheetByName("Responsible");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range8 = sheet.getRange("K3:K90");
  var range28 = sheet.getRange("M3:M90");
  var range2values8 = range28.getValues();
  var rangevalues8 = range8.getValues();
  //Loops through range results
  for (var i in rangevalues8) {
    // for (var j in rangevalues) {
    Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added

    //Set the rules logic
    if (rangevalues8[i][0] == true) { //Modified
      //Set the cell 
      range2values8[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values8);//Added
    }
  }

  //copy new information
  var destination = ss.getSheetByName('Compiled Data');//whatever page
  var destCell8 = destination.getRange("I183:I270");
  destCell8.setValues(range2values8);

  //clear checkboxes
  var cleaning = ss.getSheetByName('Asset Bank');
  var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
  range8.setValues(cleaningcell8);
}

感谢我能提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

在检查完代码后,我猜您是用错误的方法getcloumn()(即

)编辑的getcolumn()
var ss = e.range.getSheet();
var sheet = ss.getSheetByName("Responsible");

e.range.getrow()

似乎是主要问题

正确的语法是:

getColumn();
getRow();

,您需要从getSheetByName(name)对象而不是spreadsheet对象中使用函数sheet

这是更正的代码:

 function onEdit(e) {

    //Get the sheet you want to work with. 
    var editrange = {
        top: 2,
        bottom: 260,
        left: 11,
        right: 11
    };
    //getRow() and not getrow()
    var thisrow = e.range.getRow();
    if (thisrow < editrange.top || thisrow > editrange.bottom) return;
    //getColumn() and not getcolumn()
    var thiscolumn = e.range.getColumn();
    if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
    //Line that replaces the erroneous 'var ss = e.range.getSheet()';
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName("Responsible");
    //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
    var range8 = sheet.getRange("K3:K90");
    var range28 = sheet.getRange("M3:M90");
    var range2values8 = range28.getValues();
    var rangevalues8 = range8.getValues();
    //Loops through range results
    for (var i in rangevalues8) {
        Logger.log("rangevalues8[" + i + "][" + 0 + "] is:" + rangevalues8[i][0]); //Added
        //Set the rules logic
        if (rangevalues8[i][0] == true) { //Modified
            //Set the cell 
            range2values8[i][0] += 1; //Directly add 1 to range2values
            Logger.log(range2values8); //Added
        }
    }
    //copy new information
    var destination = ss.getSheetByName('Compiled Data'); //whatever page
    var destCell8 = destination.getRange("I183:I270");
    destCell8.setValues(range2values8);
    //clear checkboxes
    var cleaning = ss.getSheetByName('Asset Bank');
    var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
    range8.setValues(cleaningcell8);
}