Javascript 使用应用程序脚本将表格图表从google电子表格发送到slack
我试着每天从我的谷歌电子表格发送表格图表到我的slack频道。当我创建一些普通图表(如柱状图或折线图)时,自动化会起作用,但如果我尝试使用表格图表进行相同操作,则会显示错误。我是否应该更改任何代码以使其运行Javascript 使用应用程序脚本将表格图表从google电子表格发送到slack,javascript,google-apps-script,google-sheets,slack,slack-api,Javascript,Google Apps Script,Google Sheets,Slack,Slack Api,我试着每天从我的谷歌电子表格发送表格图表到我的slack频道。当我创建一些普通图表(如柱状图或折线图)时,自动化会起作用,但如果我尝试使用表格图表进行相同操作,则会显示错误。我是否应该更改任何代码以使其运行 function myFunction(){ var sheetUrl2 = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxx/edit#gid=xxxxx" var ss2 = Sp
function myFunction(){
var sheetUrl2 = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxx/edit#gid=xxxxx"
var ss2 = SpreadsheetApp.openByUrl(sheetUrl2);
var sheet2 = ss2.getSheetByName("MyDailySheet");
var charts2 = sheet2.getCharts();
var chartImage2 = charts2[0].getBlob().getAs('image/jpeg').setName("graph.png");
sendSlack(chartImage2);
}
function sendSlack(chart){
var url = 'https://slack.com/api/files.upload';
var token = 'xoxp-xxxxxxxxxxxxxxxxxxxxxxxxxx';
var channel = '#general';
var payload = {
'token' : token,
'channels' : channel,
'file' : chart,
'filename' : 'DailyUsers - Last 30 Days'
};
var params = {
'method' : 'post',
'payload' : payload
};
var response = UrlFetchApp.fetch(url, params);
}
错误消息:
Exception: Service Spreadsheets failed while accessing document with id xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
myFunction @ DailyChart02.gs:7
这段代码运行良好,并将图表发送到我的空闲通道
我的slack应用已授权以下令牌作用域:(bot)chat:write
文件:write
(用户)聊天:write
文件:write
我的图表看起来像这样。它完美地显示在我的电子表格文件中。
问题和解决方法:
我和你有过同样的情况。在当前阶段,表图表似乎无法作为图像blob直接检索。这可能是当前的规范。因此,作为当前的解决方法,我想提出以下流程。在这个解决方案中,谷歌幻灯片被用作包装器。顺便说一下,在脚本中,使用了getAs('image/jpeg')
。但似乎使用了setName(“graph.png”)
。因此在这次修改中,mimeType使用文件名中的image/png
function myFunction(){
var sheetUrl2 = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxx/edit#gid=xxxxx"
var ss2 = SpreadsheetApp.openByUrl(sheetUrl2);
var sheet2 = ss2.getSheetByName("MyDailySheet");
var charts2 = sheet2.getCharts();
// --- I added below script.
var s = SlidesApp.create("temporalSlides");
var chartImage2 = s.getSlides()[0].insertSheetsChartAsImage(charts2[0]).getBlob().setName("graph.png");
DriveApp.getFileById(s.getId()).setTrashed(true);
// ---
sendSlack(chartImage2);
}
修改脚本:
请修改myFunction()
,如下所示
function myFunction(){
var sheetUrl2 = "https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxxxxxx/edit#gid=xxxxx"
var ss2 = SpreadsheetApp.openByUrl(sheetUrl2);
var sheet2 = ss2.getSheetByName("MyDailySheet");
var charts2 = sheet2.getCharts();
// --- I added below script.
var s = SlidesApp.create("temporalSlides");
var chartImage2 = s.getSlides()[0].insertSheetsChartAsImage(charts2[0]).getBlob().setName("graph.png");
DriveApp.getFileById(s.getId()).setTrashed(true);
// ---
sendSlack(chartImage2);
}
注:
- 在这种情况下,作为未来的请求向Google issue tracker报告如何李>