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

如何通过Google Apps Script设置Google Sheets图表横纵轴的数字格式

你可以通过添加vAxis.format和hAxis.format配置项实现轴标签数字格式自定义,不需要修改源数据,该参数属于未被官方文档完全收录的隐藏配置项,格式规则和Google Sheets内置的数字格式规则一致。

如果要将纵轴标签设置为整数,只需要在原有代码基础上新增一行配置:
embeddedChartBuilder.setOption('vAxis.format', '#')

如果横轴同样为数字类型需要显示整数,新增对应横轴配置即可:
embeddedChartBuilder.setOption('hAxis.format', '#')

如果有其他格式需求,只需替换引号内的格式代码即可:

  • 显示带千分位的整数:#,##0
  • 整数后带固定单位(比如"次"):#次
  • 保留1位小数:#.0

修改后的完整代码示例:

embeddedChartBuilder.setChartType(Charts.ChartType.LINE)
    .setPosition(chartRow, chartColumn, 0, 0)
    .setNumHeaders(1)
    .setOption('backgroundColor', '#222222')
    .setOption('height', chartHeightPx)
    .setOption('width', chartWidthPx)
    .setOption('series', [
      {color:'#4ebcbb', pointSize:6, lineWidth:4},
      {color:'#cccccc', pointSize:6, lineWidth:4},
      {color:'#666666', pointSize:6, lineWidth:4},
      {color:'#34a853', pointSize:6, lineWidth:4}])
    .setOption('applyAggregateData', 0)
    .setOption('vAxis.gridlines.color', '#434343')
    .setOption('vAxis.minValue', 0)
    .setOption('vAxis.maxValue', 5)
    // 新增纵轴整数格式配置
    .setOption('vAxis.format', '#')
    .setOption('vAxis.textStyle', { color: '#efefef', fontName: 'Arial', fontSize: 12, bold: false, italic: false })
    // 若横轴需要整数格式取消注释下一行即可
    // .setOption('hAxis.format', '#')
    .setOption('hAxis.textStyle', { color: '#efefef', fontName: 'Arial', fontSize: 12, bold: false, italic: false });

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 11:45:03