I am trying to export a google chart into a google app script web app. I can send normal strings as an ajax request and create a docs file with the input of the parameters.
My idea was now to call chart.getImageURI() and send the uri with an ajax request to the google app script. In the google script I want to get the image with UrlFetchApp.fetch(img_uri).
The problem is that I always get errors from the ajax request with jquery. The error is: GET 'url' net::ERR_CONNECTION_CLOSED. I tried to shorten the url with bit.ly but there is also an error, that the url is to long.
I hope somebody can help me or knows another way how to solve this problem! Thanks in advance.
Code:
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);
}
And the code that creates the chart:
// 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);
The google script:
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);
}
I can't even add the created url from ajax here because the characters are limited and the url is to big to add it here.