Format a Google Sheets cell in plaintext via Apps Script

The other answer, to set the format to 'plain text' in javascript, doesn't work. However, this does:

sheet.getRange(1,n).setNumberFormat('@STRING@');

So the magic value for formatting text programmatically is '@STRING@'!


A complement to Christian good answer : I have noticed that if your target cell has already been set to text format (manually), setting a value with range.setValue() that might not been interpreted as a text (like a date, or a number), will change automatically the number format. So the good solution, if you want definitely to keep a text format is

range.setValue("01293849847").numberFormat("@");

and not

range.numberFormat("@").setValue("01293849847");


Another way to force the value to be saved as plain text:

Prepend a quotation mark: '. This has no effect on displayed value and also by reading the value back the quatation mark is not included.

let dateStr = '07-07-20017';
sheet.getRange(x, y).setValue("'" + dateStr);

let backStr = sheet.getRange(x, y).getValue();
assert(dateStr === backStr);

Unfortunately the text of the plan did not fit. Document as soon as the cell gets something like a date, just format the cell. Found a way not to create date)

var thisDate1 = Utilities.formatDate (new Date (), SpreadsheetApp.getActiveSpreadsheet (). getSpreadsheetTimeZone (), "MM//d//yyyy");

Sorry so late reply. All hands did not reach.