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.setNumberFormats is not a function (line 15, file "Code")
. What should I change to fix this?
What I want to do is, I want to display dod1
in a percentage format like 0.00%
function notifySlack(message) {
var url = 'https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxx/edit#gid=xxxxxxxxx';
var spreadSheet = SpreadsheetApp.openByUrl(url);
var allSheets = spreadSheet.getSheets();
var theSheet = allSheets[1];
var lastRow = theSheet.getLastRow();
var lastColumn = theSheet.getLastColumn();
var sheetData = theSheet.getSheetValues(1, 1, lastRow, lastColumn);
var ymd = Utilities.formatDate(sheetData[44][0], 'Asia/Tokyo', 'yyyy-MM-dd');
var data1 = sheetData[44][1];
var percent1 = sheetData[44][1] / sheetData[43][1];
var dod1 = percent1.setNumberFormat('0.00%');
}
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
.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.
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.
var dod1 = percent1.setNumberFormat('0.00%');
To:
var dod1 = Utilities.formatString("%1.2f%", percent1 * 100);
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%');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.