Search code examples
google-sheets-apigoogle-docs-api

Using Google SpreadsheetApp setNumberFormat, setting format as percent and not multiplying by 100?


I'm building a Google Spreadsheet from Google Analytics data using the APIs, and I'm trying to format some numbers.
Based on the Sheets API Format page, I should be able to use .setNumberFormat("00.00\%"); to set the number to two decimal places and clear the % with the slash so that it won't run it's multiplier on the number. But it is not working as expected. According to that page, the \ character "Treats the next character as a literal value and not any special meaning it might have."

RAW DATA: 33.0923537840302
GOAL: 33.09%
ACTUAL RESULT: 3309.23%

If I use .setNumberFormat("00.00"); I get the result I want, but it lacks the % on the end.


Solution

  • You can try this:

    cell.setNumberFormat("00.00");
    cell.setValue(cell.getValue() + "%")
    

    First you'll set the format to "00.00" then concatenate the percent sign to the format result.

    enter image description here

    Hope this workaround helps.