Slice() failing on date datatype column

170 views
Skip to first unread message

Octávio Teixeira

unread,
Apr 20, 2023, 11:30:32 AM4/20/23
to Google Apps Script Community
Hello,
I am new to appscript and working on a standalone script. I am reading data from a google sheet. The code works fine if I use 4 columns (text datatypes). When I include the fifth column (date datatype) on ws.getRange I get "Uncaught TypeError: Cannot read properties of null (reading 'slice')" on the browser console. 
Function setDadosFinal() is on a tag on an html file.
Function dadosParaProcura() is on a separate .gs file. 
Many thanks for any help.

Code:

function dadosParaProcura() 
const ws = ss.getSheetByName("Registos"); 
return ws.getRange(2,1,ws.getLastRow()-1,5).getValues();  }


var data;     <!--Global--> 
function setDadosFinal(){ google.script.run.withSuccessHandler(function(dataReturned){ data = dataReturned.slice(); }).getDataForSearch(); }




Tanaike

unread,
Apr 20, 2023, 8:27:18 PM4/20/23
to Google Apps Script Community
From `The code works fine if I use 4 columns (text datatypes). When I include the fifth column (date datatype) on ws.getRange I get "Uncaught TypeError: Cannot read properties of null (reading 'slice')" on the browser console.`, I think that the reason for your current issue is due to the following specification.


https://developers.google.com/apps-script/guides/html/reference/run#parameters
Most types are legal, but not Date, Function, or DOM element besides form; see description

Legal parameters 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. A form element within the page is also legal as a parameter, but it must be the function’s only parameter. 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. Objects that create circular references will also fail, and undefined fields within arrays become null. Note that an object passed to the server becomes a copy of the original. If a server function receives an object and changes its properties, the properties on the client are not affected.



In the current stage, it seems that the date object cannot be sent from Google Apps Script to Javascript using google.script.run. So, in this case, how about the following modification?

From

return ws.getRange(2,1,ws.getLastRow()-1,5).getValues();

To

return ws.getRange(2,1,ws.getLastRow()-1,5).getDisplayValues();

By this modification, the values are sent as the string type.



Reply all
Reply to author
Forward
0 new messages