问题
Tl;Dr. Is a good practice chaining getValues()
to Class Sheet getActiveRange()
? What could cause that sometimes returns [[]]
instead of the expected values?
NOTE: [[]]
is what is being displayed in the Log / Script executions page. These "things" doesn't show quotation characters for strings.
This is derived from Get selected values in row where I posted an answer with a couple of alternatives to get the values of the active range.
Here I'm specifically asking for the reasons of the randomly failure of the following code
function myFunction2(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var values = sheet.getActiveRange().getValues();
Logger.log(values);
}
Steps that I followed when the failure ocurred
- Create a new spreadsheet
- Add some values to a row
- Select the row
Click on the corresponding row heading - Click on Tools > Script editor This creates a bounded project that uses the new runtime (Chrome V8) a default Google Cloud Project for Google Apps Script
- Add a simple function:
function myFunction() {
var values = SpreadsheetApp.getActiveRange().getValues();
Logger.log(values);
}
- Run myFunction to authorize the script
- Run myFunction to actually execute myFunction
- Press Ctrl + Enter to open the Log
- Add the referred function at the beginning of this question (
myFunction2
) - Run
myFunction2
- Open the Log using the referred keyboard shortcut (step 7)
[[]]
were logged instead of the expected values.
- Add a third function
function myFunction3(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var range = sheet.getActiveRange();
var values = range.getValues();
Logger.log(values);
}
- Run
myFunction3
- Open the Log using the referred keyboard shortcut
- Run again
myFunction2
- Open the Log using the referred keyboard shortcut (step 7). Now the expected values were logged.
The following are questions that use Class Sheet getActiveRange()
chained with some Class Range methods like getRow()
, getValues()
but the current answers doesn't mention the cause of the problem, they just offer an alternative code
- Emailing data from a spreadsheet at a specific time of day
- Convert row and column data to column-only
- Trying to copy values from another tab but keep getting error "function getValues() can not be used as the left-hand side"
I already searched the Issue Tracker. While there are some issues related to getActiveRange like Calls to .getActiveRange() don't return correct cells as seen in filter views they don't appear to be directly related to this issue.
As of Aug, 9, 2020 (UTC) the same spreadsheet / bounded script are returning the expected values, anyway I added another funtion to test getValues()
, getRow()
, getColumn()
, getA1Notation()
and getGridI()
. All returned the expected values.
I just found another question about a similar problem with getActiveRange()
-> getActiveRange not returning current selection. This question is not a duplicate because that question only mentions that after 24hours it worked again but didn't mentions if it's a a good practice to chain Class Range methods to getActiveRange()
and the currents answers doesn't explains why this happens.
回答1:
So far my conclusion based on the comments to the question and on the related questions that used chaining and the answers uses equivalent code but that doesn't use chaining I think that is safer to avoid the use of chaining with "active" methods.
By active methods I mean
- Class Spreadsheet
getActiveSheet()
- Class Spreadsheet and Class Sheet
getActiveRange()
Related chat rooms
- chat created by me
- chat created by Oleg Valter
- chat created by a ♦ mod.
来源:https://stackoverflow.com/questions/63321360/why-class-range-getvalues-sometimes-returns-when-chained-to-class-sheet-get