0

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.

ebimanuel
  • 186
  • 1
  • 4

1 Answers1

0

You need to post the doPost() function from your google script. However, you cannot use JSONP with a POST request. Check here: Unable to post data using JSONP on Cross Domain

Use GET instead.

Community
  • 1
  • 1
Sujay Phadke
  • 2,145
  • 1
  • 22
  • 41
  • I only implemented the doGet(), i changed method to POST because I read that you can send larger data with it, normally I am using GET but forgot to change it back. But I think the problem is not in the script, but rather in the ajax call. – ebimanuel Mar 02 '16 at 03:11
  • well post the doGet() and the version of your code using doGet then. – Sujay Phadke Mar 02 '16 at 03:26
  • I added the doGet() in my question above and changed the ajax call like I normally use it. – ebimanuel Mar 02 '16 at 03:29
  • I think the main problem is that the created url from ajax is to long to get executed for the google api, but i don't know how I can solve this problem or I have to find another solution how I export a chart into the GAS app. – ebimanuel Mar 02 '16 at 03:40