Skip to content Skip to sidebar Skip to footer

.setNumberFormat Is Not Working In Google Apps Script

I have the following code in my Google Apps Script which gets data from my Google Spreadsheet, and row 15 (the last row) returns an error saying TypeError: percent1.setNumberFormat

Solution 1:

Modification points:

  • In your script, when the values of sheetData[44][1] and sheetData[43][1] are the numbers, percent1 of var percent1 = sheetData[44][1] / sheetData[43][1]; is the number. When those are the string, percent1 is NaN.
  • And, setNumberFormat is the method of Class Class Range. Ref

By these situation, your script occurs the error. This is the reason of your issue. Unfortunately, from your script, I couldn't understand about the detail of your goal. So in this answer, I would like to propose 2 patterns for your goal I guessed.

Pattern 1:

In this pattern, the value of percent1 of var percent1 = sheetData[44][1] / sheetData[43][1]; is modified to 0.00%. In this case, the value of dod1 becomes a string.

From:
var dod1 = percent1.setNumberFormat('0.00%');
To:
var dod1 = Utilities.formatString("%1.2f%", percent1 * 100);

Pattern 2:

In this pattern, the method for using setNumberFormat is explained. When you want to modify the number format of the cell "A1" in the active sheet, you can use the following script. In this case, the value of the cell "A1" can be used as the number.

var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1");
range.setNumberFormat('0.00%');

References:


Post a Comment for ".setNumberFormat Is Not Working In Google Apps Script"