在Google Spreadsheet AppScript中调用round函数报错,该如何解决?
解决Code.gs中调用round方法报错的问题
- 你无法直接在Code.gs里调用Google Spreadsheet的工作表函数
round(),得用标准调用方式:- 通过SpreadsheetApp调用工作表ROUND函数
如果需要给单元格设置公式实现round计算,用这种写法:// 示例:给Sheet1的A1单元格设置公式,计算B1的值并保留2位小数 SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").setFormula('=ROUND(B1, 2)'); - 用JavaScript内置的Math.round()
如果是在脚本里直接处理数值(不需要写入单元格公式),用JS自带的方法更高效:// 示例:将数值保留2位小数 let targetNum = 123.456; let roundedResult = Math.round(targetNum * 100) / 100; // 结果为123.46
- 通过SpreadsheetApp调用工作表ROUND函数
之前能正常运行大概率是代码里曾自定义过round函数,或者存在兼容旧逻辑的写法,但现在必须用上述标准方式实现需求。
内容的提问来源于stack exchange,提问作者Alex
相关产品推荐
相关产品推荐

