0

I have an HTML page that produces data (a table row). I want to store rows from all clients in an online table which can be accessed/downloaded (preferably by the owner alone, so anonymous clients can only add rows)

Possible solutions and encountered problems:

  • Google spreadsheet + google apps script - How to do a cross origin POST request?
  • Google fusion tables - How to add rows from anonymous clients? Is that possible?
  • Google app engine - possible, but seems too time consuming for this simple task.
Uri
  • 25,622
  • 10
  • 45
  • 72
  • If your app has a backend, ala appengine, you can easily post to google spreadsheets. Otherwise, you can `post` to any spreadsheet if the google apps script is setup to handle a `doPost`. – rGil Jun 01 '13 at 04:41
  • I don't have a backend and I want to refrain from using one. As for POST requests to apps script, this could work only that I need to do them cross domain - I'm not sure how to do that. – Uri Jun 01 '13 at 10:10

1 Answers1

0

I've found an answer on how to do cross domain POST, so I've managed to do what I want with apps script + spreadsheet:

Apps script:

function doPost(request) {
    var ss = SpreadsheetApp.openById(id);
    var sheet = ss.getSheets()[0];
    if (request.parameters.row != null) {
      sheet.appendRow(Utilities.jsonParse(request.parameters.row));
    }
}

Client javascript (from https://stackoverflow.com/a/6169703/378594):

function crossDomainPost(paramsDict, url) {
    // Add the iframe with a unique name
    var iframe = document.createElement("iframe");
    var uniqueString = "SOME_UNIQUE_STRING";
    document.body.appendChild(iframe);
    iframe.style.display = "none";
    iframe.contentWindow.name = uniqueString;

    // construct a form with hidden inputs, targeting the iframe
    var form = document.createElement("form");
    form.target = uniqueString;
    form.action = url;
    form.method = "POST";

    // repeat for each parameter
    for (i in paramsDict) {
        var input = document.createElement("input");
        input.type = "hidden";
        input.name = i;
        input.value = paramsDict[i];
        form.appendChild(input);   
    }

    document.body.appendChild(form);
    form.submit();
}

crossDomainPost({'row': JSON.stringify([123, 123, 1211])}, serverURL);

Note that a similar scheme should work for Google fusion tables.

Community
  • 1
  • 1
Uri
  • 25,622
  • 10
  • 45
  • 72
  • 1
    I actually did this a few months ago as a proof of concept. I [put the explanation in a google doc](http://goo.gl/1oHDp) along with some code samples for both the html and the app script. I made the code public, so the internal ajaxy links may no longer work, but it should help. – rGil Jun 01 '13 at 12:25