Google Apps Script的`getValues`方法如果任何单元格包含日期会返回null。

huangapple go评论84阅读模式
英文:

Google Apps Script range getValues returns null if any cell contains a date

问题

我正在尝试获取范围数值。我注意到,如果任何范围单元格包含日期值(例如,2000-01-01),getValues() 返回 null

SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MY_NAMED_RANGE').getValues();

但是当使用 getDisplayValues() 时,我没有问题地获取值:

SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MY_NAMED_RANGE').getDisplayValues();

这是否是 getValues() 的一个错误?
我需要获取精确/未格式化的值,因此无法使用 getDisplayValues()

提前感谢您的帮助!

英文:

I'm trying to get the range values. I noticed, if any range cell contains a date value (for example, 2000-01-01), getValues() returns null:

SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MY_NAMED_RANGE').getValues();

But when using getDisplayValues() I get the values no problem

SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MY_NAMED_RANGE').getDisplayValues();

Is it a bug of getValues()?
I need to get the precise/unformatted values, so I cannot use getDisplayValues()

Thanks in advance for your help!


Thank you all for your replies. I'm trying to get some named range data in my Google Sheets custom sidebar, here is my simplified example:

So I use this (backend) Google Script function (getting the values of a named range):

function gsExplorationData(){
    var range=SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MyNamedRange');
    Logger.log({'range.getValues()': range.getValues()});
    return {'range.getValues()': range.getValues()}
}

Here is my Sidebar javascript function:

dataSubmit(){
  google.script.run.withFailureHandler(function(error){console.log('GS ERROR: '+ error.message)}).withSuccessHandler(function(gsResponse){
    console.log('gsExplorationData()',gsResponse);
  }).gsGetgsExplorationDataData()
}

When running dataSubmit() I see the results fine in the Executions logs:
cloud logs

BUT the dataSubmit() outputs null in the DEV tools:
getting Null in the sidebar javascript

Now if I simply remove the date value in Cell B16 from my named range, I get the range values in the sidebar javascript function no problem:
no date values

Or if change my google script function to return getDisplayValue(), it also works:

function gsExplorationData(){
    var range=SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MyNamedRange');
    Logger.log({'range.getValues()': range.getValues()});
    return {'range.getDisplayValues()': range.getDisplayValues()}
}

getDisplayValues

It also works if I stringify getValues:

function gsExplorationData(){
    var range=SpreadsheetApp.getActiveSpreadsheet().getRangeByName('MyNamedRange');
    Logger.log({'range.getValues()': range.getValues()});
    return {'range.getValues() stringified': JSON.stringify(range.getValues())}
}

stringified values

So the issue is with getting the range getValues() results (that contain date values) from a Google Sheets function in a sidebar javascript function.

UPDATE: kudos to @Daniel who pointed out to the limitations of Apps Script, in particular when returning Dates from Google Script functions called using google.script.run

> This has been reported in Google's issue tracker in the past, but it
> has been marked as "Intended behavior", so it seems that there are no
> plans to add this functionality at the moment. It could be a limit of
> how the Apps Script client-to-server communication works, or some kind
> of security measure.
>
> As a workaround you can try to pass the dates as raw strings, and then
> convert them to Date objects on the client-side.
>
> Reference: HTML Service: Communicate with Server Functions

答案1

得分: 1

我检查了你的示例代码,这是尝试使用 google.script.run 将值传递到侧边栏的一个限制。根据文档

可接受的参数和返回值包括 JavaScript 基本类型,如数字(Number)、布尔值(Boolean)、字符串(String)或 null,以及由基本类型、对象和数组组成的 JavaScript 对象和数组。

(...)

如果尝试传递日期(Date)、函数(Function)、DOM 元素(除了表单以外的元素),或其他被禁止的类型,包括对象或数组中的被禁止类型,请求将失败。

过去在Google的问题追踪器中报告过这个问题,但被标记为“预期行为”,因此似乎没有计划在目前添加这个功能。这可能是Apps Script客户端到服务器通信方式的限制,或者是某种安全措施。

作为一种解决方法,你可以尝试将日期作为原始字符串传递,然后在客户端将它们转换为 Date 对象。

参考链接:

英文:

I checked your sample code, and this is a limitation of trying to pass the values to the sidebar with google.script.run. From the documentation:

>Legal parameters and return values are JavaScript primitives like a Number, Boolean, String, or null, as well as JavaScript objects and arrays that are composed of primitives, objects and arrays.
>
>(...)
>
>Requests fail if you attempt to pass a Date, Function, DOM element besides a form, or other prohibited type, including prohibited types inside objects or arrays.

This has been reported in Google's issue tracker in the past, but it has been marked as "Intended behavior", so it seems that there are no plans to add this functionality at the moment. It could be a limit of how the Apps Script client-to-server communication works, or some kind of security measure.

As a workaround you can try to pass the dates as raw strings, and then convert them to Date objects on the client-side.

Reference:

huangapple
  • 本文由 发表于 2023年3月12日 13:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75711181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定