0

我想出口谷歌应用程序脚本的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这里是因为字符是有限的,并且网址很大,所以在这里添加它。

+0

你需要在这里发布你的代码,以便人们不必去想象它是什么。 –

+0

谢谢,我添加了一些我认为相关的代码。 – ebimanuel

+0

您可以随时使用bit.ly来缩小URL的大小 –

回答

0

您需要从google脚本中发布doPost()函数。但是,您不能在POST请求中使用JSONP。请点击这里:Unable to post data using JSONP on Cross Domain

使用GET代替。

+0

我只实现了doGet(),我将方法更改为POST,因为我读到可以使用它发送更大的数据,通常我使用GET但忘记将其更改回来。但我认为问题不在脚本中,而是在ajax调用中。 – ebimanuel

+0

然后使用doGet发布doGet()和您的代码版本。 –

+0

我在上面的问题中添加了doGet(),并像通常使用的那样更改了ajax调用。 – ebimanuel

相关问题