1

I have a problem where my Google Script function is not passing in date values to my JavaScript. Text and numerical values however are being passed in. The Google Scripts function searches my google sheets document to find a row of values based on a value that is passed into it. It then takes the data, puts it into an array and ships it over to my JavaScript function. The JavaScript then assigns the values from the array to my HTML document.

Here is my JavaScript:

  function callDataRetriever(){
    var number = document.getElementById("number").value;
    google.script.run.withSuccessHandler(dataRetriever).retreiveData(number);
  }

  function dataRetriever(data){
    document.getElementById("location").value = data[0]; //This works
    document.getElementById("dateOpened").value = data[1]; //This does not work. Stops the function from continuing its task.        
    document.getElementById("value1").value = data[2]; //Without the date everything here down works
    document.getElementById("value2").value = data[3];
    document.getElementById("value2").value = data[4];
    document.getElementById("value4").value = data[5];
    //...
  }

Here is my Google Scripts:

function retreiveData(number){
  var url = "urlToSpreadsheet";
  var ss = SpreadsheetApp.openByUrl(url);
  var ws = ss.getSheetByName("Data");
  var data = ws.getRange(1,1, ws.getLastRow(), ws.getLastColumn()).getValues();
  var dataValues = [];
  var filterData = data.filter(
    function(r){
      if(r[2] == number){
        var i = 3;
        while(i < 29){
          dataValues.push(r[i]);  
          i++;
        }
      }
    }
  )
  return dataValues;
}

In my Logs this is how it looks:

enter image description here

It is grabbing the date correctly however once passed into my JavaScript the function ceases to continue.

UPDATE: Edited code based on doubleunary's suggestion. Now getting an error that I do not fully understand:

enter image description here

1
  • Try using .getDisplayValues() at the server-side and see if that works? Also check out this link. Commented May 14, 2021 at 13:55

2 Answers 2

3

You cannot pass a Date object but will have to serialize it before sending. 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. A form element within the page is also legal as a parameter, but it must be the function’s only parameter, and it is not legal as a return value. 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.

Try using Utilities.formatDate(myDate, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), 'yyyy-MM-dd') in the server side and new Date(data[1]) in the client side.

Sign up to request clarification or add additional context in comments.

2 Comments

I attempted this and it did not work, did I do it wrong? (See my edit in the post above)
The formatDate() call is missing the timezone parameter.
0

It seemed all I needed to do was add document.getElementById("dateValue").valueAsDate = new Date(data[1]); in my JavaScript and leave my Google-Script alone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.