0

In ma Google Application Script project I need to make sure that a cell data is always read as a text. To make sure that it happens like that I added .setNumberFormat('@STRING@') after each .appendRow(). To make 100% sure I even change the format after I update a cell.

But it happened that in the middle of spreadsheet there was row where the formatting was not text but .... I guess it had no formatting.

So when reading and working with such data an error was created Thu Mar 11 2021 00:00:00 GMT+0100 (Central European Standard Time) Error TypeError: data[row][1].split is not a function at getTabulatorData(web functions:445:34)

because split() function does not work on date object.

The data saved in the cell is 3/11/2021

The current code how to get data is

var range = sheetData.getRange(firstRow, 1, lastRow, columns)
var data  = range.getValues()

If I use

var dataTMP = Sheets.Spreadsheets.Values.get(spreadsheetId, sheetName+"!"+range.getA1Notation(),
                                                         {valueRenderOption:"FORMATTED_VALUE"})

then I get correct text value even I format the cell as a date.

Is this correct way to read data as text? In Google documentation is written

// This code uses the Sheets Advanced Service, but for most use cases
// the built-in method SpreadsheetApp.getActiveSpreadsheet()
//     .getRange(range).getValues(values) is more appropriate.
var result = Sheets.Spreadsheets.Values.get(spreadsheetId, range);
var numRows = result.values ? result.values.length : 0;

Question 1: Does Sheets.Spreadsheets.Values.get(spreadsheetId,sheetName+"!"+range.getA1Notation(),{valueRenderOption:"FORMATTED_VALUE"}) give me always save data as a raw text?

Question 2: Is there a way for .getRange().getValues() how to read, interpret save data?

Radek
  • 13,813
  • 52
  • 161
  • 255

1 Answers1

3

Try using .getDisplayValues() instead of .getValues(). The first will return the values as strings.

JPV
  • 26,499
  • 4
  • 33
  • 48