Why Class Range getValues sometimes returns [[]] when chained to Class Sheet getActiveRange?

隐身守侯 提交于 2020-08-26 10:35:26

问题


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

  1. Create a new spreadsheet
  2. Add some values to a row
  3. Select the row
    Click on the corresponding row heading
  4. 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
  5. Add a simple function:
function myFunction() {
  var values = SpreadsheetApp.getActiveRange().getValues();
  Logger.log(values);
}
  1. Run myFunction to authorize the script
  2. Run myFunction to actually execute myFunction
  3. Press Ctrl + Enter to open the Log
  4. Add the referred function at the beginning of this question (myFunction2)
  5. Run myFunction2
  6. Open the Log using the referred keyboard shortcut (step 7) [[]] were logged instead of the expected values.

  1. Add a third function
function myFunction3(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var range = sheet.getActiveRange();
  var values = range.getValues();
  Logger.log(values);
}
  1. Run myFunction3
  2. Open the Log using the referred keyboard shortcut
  3. Run again myFunction2
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!