简体   繁体   English

通过 JS 脚本和 Google 表格将图表抓取为图像 API?

[英]Grabbing a Chart as image via JS script and Google Sheets API?

I'm creating a Discord Bot that will grab a specified chart from a Google Sheet and send the chart to the server.我正在创建一个 Discord 机器人,它将从 Google 表格中获取指定的图表并将图表发送到服务器。 The sheet on hand will only ever have one chart at a time, so I've been able to find the correct chartId as follows:手头的工作表一次只会有一个图表,所以我已经能够找到正确的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;
});

However, I cannot find out how to export the corresponding chart as an image, or find a unique URL to the chart, or even recreate the chart using the existing data.但是,我找不到如何将相应的图表导出为图像,或找到图表的唯一 URL,甚至无法使用现有数据重新创建图表。 I'm using discord.js and Google Sheets API v4.我正在使用 discord.js 和 Google 表格 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.不幸的是,在这篇文章发布时,无法将图表检索为图像或使用表格 API 获取其特定的 URL。在这里,当我们遇到尝试检索电子表格中图表图像的问题时,我将提供三种解决方法.

Workaround 1解决方法 1

If your main intention is to get the chart data to then use it somewhere else or create a data Blob with it for other Google Applications, you can use the API method GET requesting only the desired fields (in our case, the chart objects) as shown in this example of the documentation .如果您的主要目的是获取图表数据然后在其他地方使用它或为其他 Google 应用程序创建一个数据Blob ,您可以使用 API 方法GET只请求所需的字段(在我们的例子中是图表对象)作为显示在文档的这个例子中

Workaround 2解决方法 2

If what you need is to easily access this chart image with a simple link, you could publish your chart following these steps shown in the documentation .如果您需要通过一个简单的链接轻松访问此图表图像,您可以按照文档中显示的这些步骤发布您的图表。 This can generate either a link or an embed for your website that you can easily share and use.这可以为您的网站生成链接嵌入,您可以轻松共享和使用。

Workaround 3解决方法 3

You can use Apps Script to create a Web App and using the API method in the first workaround generate a Blob from the chart object and use it to be inserted as an image either in another sheet or in a HTML page.您可以使用 Apps 脚本创建一个 Web 应用程序,并在第一个解决方法中使用 API 方法从图表 object 生成一个 Blob,并将其作为图像插入到另一张工作表或 HTML 页面中。 In this Stack Overflow answer you can see an exmple of the implementation of this workaround.此 Stack Overflow 答案中,您可以看到此解决方法的实施示例。

I hope this has helped you.我希望这对你有帮助。 Let me know if you need anything else or if you did not understood something.让我知道您是否需要其他任何东西或者您不明白什么。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM