getActiveRange not returning current selection

后端 未结 3 679
情歌与酒
情歌与酒 2021-01-24 17:51

This should be a simple one, but I could not crack it myself... I want to copy the currently selected cells in the active sheet in an array called data:

var shee         


        
相关标签:
3条回答
  • 2021-01-24 18:20

    It looks like the problem was on Google's side because after 24 hours of failure the existing code now works flawlessly. All your versions work fine too now.

    0 讨论(0)
  • 2021-01-24 18:23

    I think that the problem is

    var sheet = SpreadsheetApp.getActive().getActiveSheet();
    

    This because there is some kind of bug when "chaining" methods of two different Object Classes, in this case Class Spreadsheet and Class Sheet. There is a bug report related to this getActiveSheet() returns first sheet name

    The workaround is to replace the above line with:

    var spreadsheet = SpreadsheetApp.getActive();
    var sheet = spreadsheet.getActiveSheet();
    

    Related

    • Why Class Range getValues sometimes returns [[]] when chained to Class Sheet getActiveRange?
    0 讨论(0)
  • 2021-01-24 18:43

    This gets and displays the active range in a dialog window.

    function getARange(){
      var ss=SpreadsheetApp.getActive();
      var sh=ss.getActiveSheet();
      var rg=sh.getActiveRange();
      var vA=rg.getValues();
      var s='';
      for(var i=0;i<vA.length;i++){
        s+=Utilities.formatString('<br />%s', vA[i].join(','));
      }
      var userInterface=HtmlService.createHtmlOutput(s);
      SpreadsheetApp.getUi().showModelessDialog(userInterface, 'The Active Range')
    }
    
    0 讨论(0)
提交回复
热议问题