如何通过Google Apps Script导出Google Sheets计分卡时保留自定义基线颜色
Google Apps Script导出计分卡图表时自定义基线颜色失效
问题描述
我在Google Sheets中创建了一个计分卡图表,为正负基线值设置了自定义颜色(正基线设为红色),但通过Google Apps Script编程导出该图表时,导出的PNG仅显示默认颜色(正基线为绿色)。

预期结果
导出的PNG文件中,正基线值应显示为红色。
实际结果
导出的PNG文件中,正基线值显示为默认的绿色。

复现步骤
- 创建新的Google Sheets电子表格,填入以下数据:
Value Baseline 100 50 - 选中这四个数据单元格,点击顶部菜单的Insert > Chart。
- 在Chart editor面板的Setup标签页中,从Chart type下拉列表选择Scorecard chart。
- 在Chart editor面板的Customize标签页的Baseline Value部分,将Positive color改为红色,Negative color改为绿色。
- 在Chart editor面板的Customize标签页的Chart & axis titles部分,将图表标题设置为“Scorecard with red baseline color”。
- 点击顶部菜单的Extensions > Apps Script。
- 将Code.gs文件内容替换为以下代码:
const sheetName = 'Sheet1' function onOpen() { var ui = SpreadsheetApp.getUi(); ui.createMenu('Charts') .addItem('Export to Drive', 'exportCharts') .addToUi(); } function exportCharts() { const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); const sheet = spreadsheet.getSheetByName(sheetName); const charts = sheet.getCharts(); charts.forEach((chart) => { const image = chart.getAs('image/png'); const title = chart.getOptions().get('title'); DriveApp.createFile(image.setName(`${title}.png`)); }); }
- 在左侧菜单的Services处点击**+符号,添加Drive**服务。
- 点击磁盘图标保存脚本,返回Google Sheet并刷新页面。
- 页面加载几秒后,顶部菜单会出现新选项;点击Charts > Export to Drive(会提示授予脚本访问Google Drive的权限)。
- 在Google Drive中搜索名为“Scorecard with red baseline color.png”的文件,打开后确认基线颜色为绿色而非红色。
内容的提问来源于stack exchange,提问作者Shaun Scovil
相关产品推荐
相关产品推荐

