Highcharts请求Google Sheets返回404错误 如何修改请求URL?
问题根因
你遇到的404错误是因为Google已正式下线旧版GData Sheets API,你请求URL里的/feeds/cells/路径属于已废弃接口,旧版Highcharts默认调用的就是这个接口,所以之前运行正常的代码会突然报错。
修复方案
- 方案1(推荐):升级Highcharts到v9.1.0及以上版本,该版本已适配Google Sheets v4 API,你只需要在原有data配置的基础上,新增
googleAPIKey参数填入你自己申请的Google Sheets API密钥即可,原有googleSpreadsheetKey(表格密钥)、googleSpreadsheetWorksheet(工作表序号)配置无需修改。 - 方案2(自定义修改请求URL):如果你不方便升级Highcharts版本,可以手动重写Highcharts内部的Google Sheets请求逻辑,示例代码如下:
// 替换Highcharts默认的Google Sheets请求规则 Highcharts.Data.prototype.requestGoogleSpreadsheet = function (opts) { const sheetKey = opts.googleSpreadsheetKey const sheetId = opts.googleSpreadsheetWorksheet || 1 const apiKey = '你自己申请的Google Sheets API密钥' // 新版v4接口请求URL const requestUrl = `https://sheets.googleapis.com/v4/spreadsheets/${sheetKey}/values/${sheetId}?key=${apiKey}` // 发起请求处理返回数据 fetch(requestUrl) .then(res => res.json()) .then(data => { // 直接把接口返回的values二维数组传给Highcharts渲染 this.options.data.rows = data.values this.parsers.csv() this.init() }) }
- 前置要求:你需要先在Google Cloud控制台为你的API密钥开启Google Sheets API调用权限,同时对应的Google表格需要设置为公开可查看权限,否则会返回权限类错误。
内容的提问来源于stack exchange,提问作者Alex Cuadra
相关产品推荐
相关产品推荐

