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!
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.
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?
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')
}
Related
I know this has been asked before but none of the answers seem to be working for me. I need to get the values of cells that are clicked on. As a very basic prototype, I created a script tied to my spreadsheet with the following:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getValue());
}
I then go into my spreadsheet and select a cell with a value and run the function in the script window. No value is displayed in the log.
Next I tried:
function getVal() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Logger.log(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and run the function in the script window. The log displays 1,1.
And finally I tried
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
Browser.msgBox(sheet.getActiveCell().getRow() + ',' + sheet.getActiveCell().getColumn());
}
I then go into my spreadsheet and select a cell with a value (G6) and make a change to it. When I hit enter, the popup dialog displays 1,1.
This seems like it should be really simple but for the life of me cannot figure it out. Can anyone help?
I tested this in a spreadsheet and I found that using var sheet = ss.getActiveSheet(); yielded the correct results whereas using the .getSheets()[] method did not.
This is a bit late, but I was researching this today, and wanted to share: My issue was similar, in that sheet.getActiveRange().getRow(), sheet.getActiveCell().getRow(), etc., all returned row 1, regardless of what cell or range was active.
In this case, I was opening the script from a previous bookmark I created. Opening the script from a bookmark apparently doesn't re-establish the active relationship with the sheet.
Once I opened the script from the spreadsheet instead, my code returned the correct row reference.
I have tried following code and works fine. getRow() method is of Range class not Sheet class.
var ActSpSheet = SpreadsheetApp.getActiveSpreadsheet();
var ActSheet = ActSpSheet.getActiveSheet();
var GetCell = ActSheet.getCurrentCell();
var GetCurRow = GetCell.getRow();
Logger.log(GetCurRow);
Sorry for the long subject line, but this site wouldn't accept a more concise statement.
I have a Google Sheet, that has nothing more than my name in Cell A1.
Then I went to Tools / Script Editor, and have a "Code.gs" script that says:
function onEdit(e) {
Logger.log("We're in the function.");
var ss = Spreadsheet.App.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var valueOfCell = sheet.getRange(1,1).getDisplayValue();
Logger.log("Value of Cell is '" + valueOfCell + "'.");
}
When I edit my very simple sheet, and check the log (View / Logs), I get:
[19-02-28 08:58:10:182 CST] We're in the function.
That's it. I've tried every permutation I can think of or suss from three days of web-searching, and I simply can not make the .getValue() (or .getDisplayValue()) work.
What am I doing wrong?
Thanks!
/Kent
"Spreadsheet.App" should be "SpreadsheetApp".
Additional permutations that work:
Logger.log("We're in the function.");
var ss = SpreadsheetApp.getActiveSheet();
var valueOfCell = ss.getRange(1,1).getDisplayValue;
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(1,2).getDisplayValue();
and
Logger.log("We're in the function.");
var valueOfCell = SpreadsheetApp.getActiveSheet().getRange(1,2).getDisplayValue();
After four days, I think I finally understand some of this. For others as dense as I, I leave this documentation here:
Spreadsheet is the "workbook", the collection of Sheet tabs
Sheet is the individual tab'd sheet in a Spreadsheet "workbook".
Seems like simple enough information, but I couldn't find this documented anywhere.
SpreadsheetApp is the Class, the "parent" of all the Spreadsheets. While probably not technically accurate, for purposes of my thinking, I think of "SpreadsheetApp" as being the "Sheets" application within Google Docs, as opposed to "Docs" or "Slides".
So,
var ThisApp = SpreadsheetApp;
// Sets the variable "ThisApp" to "SpreadsheetApp", which I guess is the name/identification of the spreadsheet app.
var TheActiveWorkBook = SpreadsheetApp.getActive();
// Sets "TheActiveWorkBook" to the identifier for the current collection of sheets (collection of tabs within a spreadsheet).
var TheActiveWorkBook = SpreadsheetApp.getActiveSpreadsheet();
// Identical to above; just a different name for it.
var ActiveWorkBookName = SpreadsheetApp.getActive().getName();
// Sets the variable to the *name* of the workbook/spreadsheet (like, "Fred's Spreadsheet").
var ActiveSheetInWB = SpreadsheetApp.getActiveSpreadsheet();getActiveSheet();
// Sets the variable to the identifier of the active tab in the spreadsheet.
var ActiveSheetInWB = SpreadsheetApp.getActive();getActiveSheet();
// Identical to above.
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet();
// Identical to above. The active sheet is the active sheet, regardless of whether we know the active spreadsheet, so the extra step of including the active spreadsheet, as we did in the previous two examples, is unnecessary. (The system knows that the active spreadsheet is the one that contains the active sheet.)
var ActiveSheetInWB = SpreadsheetApp.getActiveSheet().getName();
// Gets the *name* of the sheet, as it shows on that sheet's tab.
var sheet = SpreadsheetApp.getActiveSheet();
var ActiveSheetInWB = sheet.getName();
// Shortens the long "SpreadsheetApp.getActiveSheet()" to a short reference to save typing in subsequent uses. Otherwise identical to above example.
I think I'm finally getting the hang of this....
I have a Google sheet that needs certain cells to be cleared at midnight every night. It has 3 tabs titled 'DOV', 'EMD', and 'Taylor's sheet', and on each of these sheets the same range needs cleared (D6:D100).
I used 'Taylor's sheet' as a test sheet because I haven't coded in forever and wanted to to make sure everything worked well first. So I implemented the clearContent() for the designated range and everything came out fine. However, once I tried adding the 'DOV' and 'EMD' sheets like my original test sheet I keep getting errors regarding the getRange function.
function ClearSheet1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Taylor's Sheet");
sheet.getRange("D6:D100").clearContent();
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss1.getSheetByName("EMD");
sheet1.getRange("D6:D100").clearContent();
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = ss2.getSheetByName("DOV");
sheet2.getRange("D6:D100").clearContent();
}
I'm confused why using getRange is returning an error
"Cannot call method 'getRange' of null."
Do I need to break each of these out into its own function?
I have a spreadsheet where every day a new column gets added with:
sheet.insertColumnAfter(6)
now i want to show/hide the columns starting at column(7) up till the last column of the sheet (which is a dynamic value).
i had the following thought and no idea why it doenst work:
function Historyausblenden() {
var bereich = SpreadsheetApp.getActive();
var letzte = bereich.getLastColumn();
bereich.getRange("G",letzte).activate();
bereich.getActiveSheet().hideColumns(bereich.getActiveRange().getColumn(), bereich.getActiveRange().getNumColumns());
bereich.getRange('F:F').activate();
}
It seems that getRange doesnt accept var letzte.
The errorcode says: "Methode getRange(string,number) not found!"
Why? What does that mean?
Several issues in your code.
You can't call getLastColumn() on a spreadsheet.
String concatenation in javascript is most easily done with + (e.g. "G" + letzte).
You can't activate a range without first selecting the sheet.
Lastly, you don't really need to activate to accomplish what you're trying to do, but you may have another reason for doing so.
Try this instead (and make sure you're selecting the correct sheet in the third line):
function hideHistory() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Sheet1");
var lastColumn = sheet.getMaxColumns();
var startColumn = 7;
sheet.hideColumns(startColumn, lastColumn-startColumn+1);
}
At the moment this is the function I'm using but I have no way of testing if it will work in the spreadsheet until I publish the application.
function readSelection() {
//The commented lines aren't needed if the sheet is open already
//var sheetid = "sheet id here";
//var spreadsheet = SpreadsheetApp.openById(sheetid);
//SpreadsheetApp.setActiveSpreadsheet(spreadsheet);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
//sheet.setActiveSelection("B2:B22");
var activerange = sheet.getActiveRange();
var activecells = activerange.getValues();
return activecells;
};
I assume you mean highlighted == selected
The result depends on whether the cells are contiguous or not (non contiguous cell selection is available in the new spreadsheets features http://googleblog.blogspot.co.nz/2013/12/new-google-sheets-faster-more-powerful.html
For contiguous cells selected your code returns the values of the selection as an array, for non-contiguous cells your code will return the an array with the single value of the LAST selected item.
I suggest that this is a bug in the implementation of the new spreadsheet. If it is important to you, I suggest you raise an issue. For the old spreadsheets, you can only select contiguous cells (eg B2:B22) so it will work as you expect.
The easiest way to answer this Q is to run the code you have written! You don't have to publish anything just run the code in the script editor of the spreadsheet you are examining
and look at the log.
function readSelection() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var activerange = sheet.getActiveRange();
var activecells = activerange.getValues();
Logger.log(activecells)
return
};
There is no way to do this at the moment or to obtain the selected ranges from a script.
A request is pending and you can support it here : https://code.google.com/p/google-apps-script-issues/issues/detail?id=4056
by adding a star to the request.
If/when this function is implemented your code would look as follows:
function readSelection() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var activeranges = sheet.getSelectedRanges();
var activecells = [] ;'
for (var ar in activeranges)
activecells = activecells.concat(activeranges[ar].getValues()) ;
Logger.log(activecells)
return ;
}
note that selected ranges may overlap, so some cell contents could be added twice.