0

I'm trying use google apps script to format a range of cells in google sheets. The cells currently have a number in them like "1000", and I want them to be formatted like "1,000 lbs". I know in sheets you can use the custom number formatting and simply use "###,0 lbs" to get the desired outcome. However, I need it to work in google apps script. Also, I don't want to just add a string " lbs" to the number, I want the range to be formatted with " lbs" in it.

I thought I could use the method setNumberFormat("#,##0 lbs") in the Range Class, but it displays the error message "Service Error: Spreadsheets". Please Help

player0
  • 124,011
  • 12
  • 67
  • 124
  • Please [edit] to expand on "does not work". Do you get a result that is not what you expect? Do you get an error message? If you were able to set the format via the UI, what did you see there? (I think the problem is that `s` is being interpreted as "seconds", which is likely a bug in Sheets. `range.setNumberFormat("#,##0 lb")` works for me.) – Mogsdad Jan 05 '16 at 14:55
  • FYI, I've added a topic to the Google Docs Help Forum for this, [here](https://productforums.google.com/forum/#!topic/docs/dub4SQloLFA;context-place=forum/docs). – Mogsdad Jan 05 '16 at 15:26
  • @Mogsdad - You are correct. It says "Service Error: Spreadsheets" when I use range.setNumberFormat("#,##0 lbs"). However, it displays as expected when using range.setNumberFormat("#,##0 lb"). – Adam Lillich Jan 18 '16 at 19:16
  • Does this answer your question? [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) – player0 Nov 08 '21 at 01:37

1 Answers1

2

There need to be quotes around lbs. And those quotes need to be escaped since they are inside a string.

setNumberFormat("#,##0 \"lbs\"")

Tesseract
  • 8,049
  • 2
  • 20
  • 37