简体   繁体   English

.setNumberFormat 在 Google Apps 脚本中不起作用

[英].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.setNumberFormats is not a function (line 15, file "Code") .我的 Google Apps 脚本中有以下代码,它从我的 Google 电子表格中获取数据,第 15 行(最后一行)返回一个错误, 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%我想要做的是,我想以0.00%之类的百分比格式显示dod1

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%');
}

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];在您的脚本中,当sheetData[44][1]sheetData[43][1]的值为数字时, var percent1 var percent1 = sheetData[44][1] / sheetData[43][1]; is the number.是数字。 When those are the string, percent1 is NaN .当这些是字符串时, percent1NaN
  • And, setNumberFormat is the method of Class Class Range.并且, setNumberFormat是 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.所以在这个答案中,我想为我猜到的你的目标提出 2 种模式。

Pattern 1:模式一:

In this pattern, the value of percent1 of var percent1 = sheetData[44][1] / sheetData[43][1];在这种模式下,var percent1 的percent1的值var percent1 = sheetData[44][1] / sheetData[43][1]; is modified to 0.00% .修改为0.00% In this case, the value of dod1 becomes a string.在这种情况下, dod1的值变成了一个字符串。

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.在本模式中,解释了使用setNumberFormat的方法。 When you want to modify the number format of the cell "A1" in the active sheet, you can use the following script.当您要修改活动工作表中单元格“A1”的数字格式时,可以使用以下脚本。 In this case, the value of the cell "A1" can be used as the number.在这种情况下,单元格“A1”的值可以用作数字。

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

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM