Reputation: 41
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 sheet = SpreadsheetApp.getActive().getActiveSheet();
var selection = sheet.getActiveRange();
var data = selection.getValues();
The above results in the array getting the content of cell A1, no matter which cells are actually selected. When I replace getActiveRange()
with e.g. getRange(2,1,10,10)
that works as expected.
Any clue as to why I cannot get to the currently selected cells is appreciated!
Upvotes: 2
Views: 1032
Reputation: 41
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.
Upvotes: 2
Reputation: 38131
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
Upvotes: 2
Reputation: 64032
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')
}
Upvotes: 0