【问题标题】:google sheets script - select multiple a1 notation range谷歌表格脚本 - 选择多个 a1 符号范围
【发布时间】:2016-01-07 15:36:09
【问题描述】:

我选择使用谷歌应用脚​​本批量更新行颜色。但是我不能使用通常的范围函数,因为要着色的行不是连续的。所以我认为,a1 表示法会有所帮助,但不幸的是,看起来我只能通过 a1 表示法的一个范围而不是多个:

var a1Notations="A1:C1,A3:C3,A10,C10";
sheet.getRange(a1Notations).setBackground("red");

但我收到“未找到范围”错误。

有什么想法可以让我完成这项工作吗?

谢谢!

【问题讨论】:

标签: google-apps-script google-sheets


【解决方案1】:

多选多个范围并更改颜色:

var sheet = SpreadsheetApp.getActiveSheet();
var rangeList = sheet.getRangeList(['A1:C1','A3:C3','A10','C10']);
sheet.setActiveRangeList(rangeList).setBackground("red");

如果您想在对话框中键入列表:

function promptRangesList() {
  var ui = SpreadsheetApp.getUi();
  var response = ui.prompt('Comma-separated ranges list', 'E.g.: A1:C1,A3:C3,A10,C10 or C3,C7,C17,C56', ui.ButtonSet.OK_CANCEL);
  Logger.log(response.getSelectedButton());
  // Process the user's response.
  if (response.getSelectedButton() == ui.Button.OK) {
    if (response.getResponseText()!=='') {
      var list = response.getResponseText().split(',');
      Logger.log(list);
      var rangeslist = SpreadsheetApp.getActiveSheet().getRangeList(list).setBackground("red");
      Logger.log(rangeslist);
      rangeslist.activate(); 
    } else {
      Logger.log('getResponseText empty');
    }    
  } else if (response.getSelectedButton() == ui.Button.CANCEL) {
    Logger.log('CANCELED');
  } else {
    Logger.log('The user clicked the close button in the dialog\'s title bar.');
  }  
}

参考:

Class RangeList - Selects the list of Range instances

Class Sheet - Sets the specified list of ranges as the active ranges in the active sheet.

Prompt dialogs - A prompt is a pre-built dialog box that opens inside a Google Docs, Sheets, or Forms editor.

【讨论】:

    【解决方案2】:

    将范围符号放入数组中,然后循环遍历数组:

    function setMultiRanges() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getActiveSheet();
    
      var a1Notations=["A1:C1","A3:C3","A10","C10"];
    
      var i=0,
          arryLngth = a1Notations.length;
    
      for (i=0;i<arryLngth;i+=1) {
        //Logger.log(a1Notations[i]);
        //Logger.log(typeof a1Notations[i]);
    
        sh.getRange(a1Notations[i]).setBackground("red");
      };
    };
    

    【讨论】:

    • 但这仍然是一一应用。我正在寻找批量更新。
    • 如果您找到解决方案,请将答案发布并标记为正确。
    【解决方案3】:

    独立设置每个范围

     sheet.getRange("A1:C1").setBackground("red");
     sheet.getRange("A3:C3").setBackground("red");
     sheet.getRange("A10:C10").setBackground("red");
    

    【讨论】:

    • 是的,这行得通,但我想一口气应用它。
    【解决方案4】:

    根据the doc of Google Sheets' API V4,选择多个范围的方式是传递一个范围数组。

    var ranges = ["'EVERYTHING'!A:A", "'EVERYTHING'!Z:Z"];
    
    // I use Node.js, so my call to the API looks like this:
    service.spreadsheets.values.batchGet({
        spreadsheetId: spreadsheetId,
        ranges: ranges
    }, function(err, result) { ...
    

    那么你得到的是一个数据数组,[{first range}, {second range}, {etc...}]

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 2018-10-26
    • 2019-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多