0

I have managed to write code to get the google spreadsheet data in JSON format. When I test the code in the script editor the dates show correctly but when I deployed and tested the link in postman.co all dates are one day behind. I mean it shows 31-Dec-2022 for 01-Jan-23, 01-Jan-23 for 02-Jan-23 and so on (one day earlier). I do not have time in the spreadsheet, by the way. How can I fix it?

Following is the script

function doGet(e) {
  var records = {};
  var rows = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).getValues();
  data = [];
  for (var r = 0, l = rows.length; r < l; r++) {
    var row = rows[r], record = {};
    record['Date'] = row[0];
    record['Branch'] = row[1];
    record['NetSale'] = row[2];
    record['Profit'] = row[3];

    data.push(record);
  }
  records.items = data;
  // Logger.log(records)
  var result = JSON.stringify(records);
  return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JSON);
}

Following is the screenshot of the Date column in the spreadsheet.

enter image description here

Codist
  • 737
  • 8
  • 23
  • Although I'm not sure whether I could correctly understand your situation, in your situation, when you use `getDisplayValues` instead of `getValues` like `var rows = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn()).getDisplayValues();`, is that your expected result? By the way, when you modified your script of Web Apps, please reflect the latest script to the Web Apps. Please be careful about this. – Tanaike Jan 29 '23 at 11:18
  • 2
    Does this answer your question? [Why is the date value result off by 1 day with Google Apps Script?](https://stackoverflow.com/questions/66194990/why-is-the-date-value-result-off-by-1-day-with-google-apps-script) – Tedinoz Jan 30 '23 at 10:03

0 Answers0