31

It is possible to set number format to a cell with google apps script like

cell.setNumberFormat("0.000");

I need to set cell format to a text, so that any date-like values were not automatically converted.
How can I do this?

Mike
  • 20,010
  • 25
  • 97
  • 140
  • 1
    Thread closed because 'This question already has answers here: [ultimate short custom number formatting - K, M, B, T, etc., Q, D, Googol](https://stackoverflow.com/questions/69773823/ultimate-short-custom-number-formatting-k-m-b-t-etc-q-d-googol)'. Am I missing something, thread started 2016 was closed because is **ALREADY** answered in a thread of 2021?.. – Mike Nov 08 '21 at 08:42

1 Answers1

79

The setNumberFormat function in the Google Apps Script is not well documented. This function accepts a wide range of parameters in a single string that can be used to specify the format for a range of cells.

So here is the different example on how to set the setNumberFormat function.

As plain text for a single cell:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Single cell
var cell = sheet.getRange("B2");

// Plain text
cell.setNumberFormat("@");

As date for single column:

// Single column
var column = sheet.getRange("B2:B");

// Simple date format
column.setNumberFormat("M/d/yy");

As currency for entire sheet:

// Entire sheet
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());

// Money format
range.setNumberFormat("$#,##0.00;$(#,##0.00)");

Source: Cell Number Formatting with Google Apps Script

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • not sure why, but the entire sheet formatting (3rd) example you show doesn't seem to work anymore. It ignores all the formats after the first semicolon! The link in the example doesn't work, either. Go figure. Sheets bug. – MC9000 Feb 09 '22 at 05:17