You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Google Spreadsheet AppScript中调用round函数报错,该如何解决?

解决Code.gs中调用round方法报错的问题
  • 你无法直接在Code.gs里调用Google Spreadsheet的工作表函数round(),得用标准调用方式:
    1. 通过SpreadsheetApp调用工作表ROUND函数
      如果需要给单元格设置公式实现round计算,用这种写法:
      // 示例:给Sheet1的A1单元格设置公式,计算B1的值并保留2位小数
      SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").setFormula('=ROUND(B1, 2)');
      
    2. 用JavaScript内置的Math.round()
      如果是在脚本里直接处理数值(不需要写入单元格公式),用JS自带的方法更高效:
      // 示例:将数值保留2位小数
      let targetNum = 123.456;
      let roundedResult = Math.round(targetNum * 100) / 100; // 结果为123.46
      

之前能正常运行大概率是代码里曾自定义过round函数,或者存在兼容旧逻辑的写法,但现在必须用上述标准方式实现需求。

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 00:32:05