我正在创建一个 Discord 机器人,它将从 Google 表格中抓取指定的图表并将该图表发送到服务器。手头的工作表一次只会有一张图表,因此我能够找到正确的 chartId
,如下所示:
sheets.spreadsheets.get({
spreadsheetId: 'MYSPREADSHEETID'
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
var id = res.data.sheets[0].charts[0].chartId;
});
但是,我不知道如何将相应的图表导出为图像,或者找到图表的唯一 URL,甚至无法使用现有数据重新创建图表。我使用的是 Discord.js 和 Google Sheets API v4。
最佳答案
At the moment of this post, unfortunately it is not possible to retrieve a chart as an image or get its specifc URL with Sheets API. Here I present three workarounds when we encounter the issue of trying to retrieve the image of a chart in a spreadsheet.
解决方法 1
如果您的主要目的是获取图表数据,然后在其他地方使用它或创建数据 Blob对于其他 Google 应用程序,您可以使用 API 方法 GET
仅请求所需的字段(在我们的示例中为图表对象),如 this example of the documentation 中所示。 .
解决方法 2
如果您需要通过简单的链接轻松访问此图表图像,您可以按照 these steps shown in the documentation 发布您的图表。这可以为您的网站生成一个链接或嵌入,您可以轻松共享和使用。
解决方法 3
您可以使用 Apps 脚本创建 Web 应用程序,并使用第一个解决方法中的 API 方法从图表对象生成 Blob,并将其用作图像插入到另一个工作表或 HTML 页面中。在 this Stack Overflow answer您可以看到此解决方法的实现示例。
希望这对您有帮助。如果您需要其他任何信息或者您不明白某些内容,请告诉我。 :)
关于javascript - 通过 JS 脚本和 Google Sheets API 抓取图表作为图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61824358/