我想出口谷歌应用程序脚本的Web应用程序的谷歌图表。我可以将普通字符串作为ajax请求发送,并通过输入参数创建文档文件。将谷歌图表导出到GAS Web应用程序与AJAX
我的想法是现在调用chart.getImageURI()并发送带有ajax请求的uri到谷歌应用程序脚本。 在谷歌脚本我想用UrlFetchApp.fetch(img_uri)来获取图像。
问题是我总是从jquery的ajax请求中得到错误。错误是:GET'url'net :: ERR_CONNECTION_CLOSED。 我试图用bit.ly缩短网址,但也有一个错误,网址很长。
我希望有人可以帮助我或知道另一种方式如何解决这个问题!提前致谢。
代码:
function publicCreateDoc(name, img_uri) {
$.ajax({
url: GOOGLE_APP_URL,
data: {
"name": name,
"img_src": img_uri
},
crossDomain: true,
type: 'GET',
dataType: 'jsonp',
success: callbackTest,
error: callbackTest
});
}
function callbackTest(params) {
console.log(params);
}
以及创建图表的代码:
// Create chart and draw it
var chart = new google.visualization.BarChart(block);
// Test
google.visualization.events.addListener(chart, 'ready', function() {
reporter.createDoc(name, chart.getImageURI());
});
chart.draw(data, options_stacked);
在谷歌脚本:
function doGet(e) {
var params = JSON.stringify(e);
// Create a new Google Doc named 'Hello, world!'
var doc = DocumentApp.create('Report for ' + e.parameter.name);
var resp = UrlFetchApp.fetch(e.parameter.img_src);
// Access the body of the document, then add a paragraph.
doc.getBody().appendParagraph('This document was created by Google Apps Script.');
doc.getBody().appendImage(resp.getBlob());
// Get the URL of the document.
var url = doc.getUrl();
// Get the email address of the active user - that's you.
var email = Session.getActiveUser().getEmail();
// Get the name of the document to use as an email subject line.
var subject = doc.getName();
// Append a new string to the "url" variable to use as an email body.
var body = 'Link to your doc: ' + url;
// Send yourself an email with a link to the document.
GmailApp.sendEmail(email, subject, body);
return ContentService
.createTextOutput(e.parameter.callback + "(" + params + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
我甚至无法从阿贾克斯添加所创建的URL这里是因为字符是有限的,并且网址很大,所以在这里添加它。
你需要在这里发布你的代码,以便人们不必去想象它是什么。 –
谢谢,我添加了一些我认为相关的代码。 – ebimanuel
您可以随时使用bit.ly来缩小URL的大小 –