Google Apps脚本表循环问题

时间:2016-09-06 18:11:21

标签: optimization google-apps-script drive

我在下面遇到了GAS代码问题。目的是遍历所有可用的工作表并创建我需要的下拉框/验证规则,以便进行易于编辑的表单。

主要问题是代码每张只运行一次,并且它永远不会将自身应用于除活动表之外的任何其他表;即。它不会循环到下一张可用的工作表。

function FailureSauce() {
  var ss = SpreadsheetApp.getActive();
  for(var n in ss.getSheets()) {  // loop over all tabs in the spreadsheet
    var sheet = ss.getSheets()[n];  // look at every sheet in spreadsheet
    var option = new Array();
    option[0]="☐";
    option[1]="☑";
    //var dv = sheet.getRange(myRange.getRow(),myRange.getColumn()+1).getValidation();  
    var dv = SpreadsheetApp.getActiveSheet().getRange(SpreadsheetApp.getActiveRange().getRow(),SpreadsheetApp.getActiveRange().getColumn()).getDataValidation();
    var dv = SpreadsheetApp.newDataValidation();
    //dv.setAllowInvalidData(false);
    dv.setAllowInvalid(false);  
    dv.setHelpText("Please choose of the options given in the drop down box");
    dv.requireValueInList(option, true);

      for (var i = 9; i <= SpreadsheetApp.getActiveSpreadsheet().getLastRow(); i++) {
        for (var y = 1; y < 4; y++) {
          SpreadsheetApp.getActiveSheet().getRange(i,y).setFontFamily("Arial")
          SpreadsheetApp.getActiveSheet().getRange(i,y).setFontSize(10)

          if (SpreadsheetApp.getActiveSheet().getRange(i,y).isBlank()) {
            //SpreadsheetApp.getActiveSheet().getRange(i,y).setValue('=if(A2=1,image("http://i.stack.imgur.com/GChKZ.jpg"),image("http://i.stack.imgur.com/yQalm.jpg"))');
            //sheet.getRange(SpreadsheetApp.getActiveSheet().getRow(),SpreadsheetApp.getActiveSheet().getColumn()).setDataValidation(dv.build());
            SpreadsheetApp.getActiveSheet().getRange(i,y).setDataValidation(dv.build());
          }
          if (SpreadsheetApp.getActiveSheet().getRange(i,y).getValues() == "a") {
            //SpreadsheetApp.getActiveSheet().getRange(i,y).setValue('=image("http://i.stack.imgur.com/GChKZ.jpg")');
            SpreadsheetApp.getActiveSheet().getRange(i,y).setDataValidation(dv.build());
            SpreadsheetApp.getActiveSheet().getRange(i,y).setValue("☑")
          }
        }
      }
    }
  }

1 个答案:

答案 0 :(得分:0)

您应该用变量SpreadsheetApp.getActiveSheet()替换sheet

function FailureSauce() {
  var dv,option,ss,sheet;

  ss = SpreadsheetApp.getActiveSpreadsheet();

  for(var n in ss.getSheets()){// loop over all tabs in the spreadsheet
    sheet = ss.getSheets()[n];// look at every sheet in spreadsheet
    Logger.log('name: ' + sheet.getName());

    option = new Array();
    option[0]="☐";
    option[1]="☑";

    //    var dv = sheet.getRange(myRange.getRow(),myRange.getColumn()+1).getValidation();  
    dv = sheet.getRange(SpreadsheetApp.getActiveRange().getRow(),SpreadsheetApp.getActiveRange().getColumn()).getDataValidation();

    dv = SpreadsheetApp.newDataValidation();
    //    dv.setAllowInvalidData(false);
    dv.setAllowInvalid(false);  
    dv.setHelpText("Please choose of the options given in the drop down box");
    dv.requireValueInList(option, true);


    for (var i = 9; i <= SpreadsheetApp.getActiveSpreadsheet().getLastRow(); i++) {
      for (var y = 1; y < 4; y++) {
        sheet.getRange(i,y).setFontFamily("Arial")
        sheet.getRange(i,y).setFontSize(10)

        if (sheet.getRange(i,y).isBlank()){
          //    sheet.getRange(i,y).setValue('=if(A2=1,image("http://i.stack.imgur.com/GChKZ.jpg"),image("http://i.stack.imgur.com/yQalm.jpg"))');
          //  sheet.getRange(sheet.getRow(),sheet.getColumn()).setDataValidation(dv.build());
          sheet.getRange(i,y).setDataValidation(dv.build());
        }
        if (sheet.getRange(i,y).getValues() == "a"){
          // sheet.getRange(i,y).setValue('=image("http://i.stack.imgur.com/GChKZ.jpg")');
          sheet.getRange(i,y).setDataValidation(dv.build());
          sheet.getRange(i,y).setValue("☑")
        }


      }
    }
  }
}