javascript - REST API Google Sheets - use from client JS -


is there way how use google sheets write , read data directly client javascript in web browser?

data security no issue here. need small free database single page web application without web server.

yes, there way. use google sheet save form data. wont give full solution, starting point.

  • create or open spreadsheet in google sheets.
  • select menu item tools > script editor. if presented welcome screen, click blank project on left start new project.
  • delete code in script editor. , copy , paste code script editor:

//  1. enter sheet name data written below          var sheet_name = "sheet1";             //  2. run > setup  //  //  3. publish > deploy web app  //    - enter project version name , click 'save new version'  //    - set security level , enable service (most execute 'me' , access 'anyone, anonymously)  //  //  4. copy 'current web app url' , post in form/script action  //  //  5. insert column names on destination sheet matching parameter names of data passing in (exactly matching case)     var script_prop = propertiesservice.getscriptproperties(); // new property service     // if don't want expose either or post methods can comment out appropriate function  function doget(e){    return handleresponse(e);  }     function dopost(e){    return handleresponse(e);  }     function handleresponse(e) {    // shortly after original solution google announced lockservice[1]    // prevents concurrent access overwritting data    // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html    // want public lock, 1 locks invocations    var lock = lockservice.getpubliclock();    lock.waitlock(30000);  // wait 30 seconds before conceding defeat.         try {      // next set write data - write multiple/alternate destinations      var doc = spreadsheetapp.openbyid(script_prop.getproperty("key"));      var sheet = doc.getsheetbyname(sheet_name);             // we'll assume header in row 1 can override header_row in get/post data      var headrow = e.parameter.header_row || 1;      var headers = sheet.getrange(1, 1, 1, sheet.getlastcolumn()).getvalues()[0];      var nextrow = sheet.getlastrow()+1; // next row      var row = [];      // loop through header columns      (i in headers){        if (headers[i] == "timestamp"){ // special case if include 'timestamp' column          row.push(new date());        } else { // else use header name data          row.push(e.parameter[headers[i]]);        }      }      // more efficient set values [][] array individually      sheet.getrange(nextrow, 1, 1, row.length).setvalues([row]);      // return json success results      return contentservice            .createtextoutput(json.stringify({"result":"success", "row": nextrow}))            .setmimetype(contentservice.mimetype.json);    } catch(e){      // if error return      return contentservice            .createtextoutput(json.stringify({"result":"error", "error": e}))            .setmimetype(contentservice.mimetype.json);    } { //release lock      lock.releaselock();    }  }     function setup() {      var doc = spreadsheetapp.getactivespreadsheet();      script_prop.setproperty("key", doc.getid());  }

    <form id="form" action="your_url">            <label>name</label><br />            <input name="name" type="text" value="" /><br />            <label>email</label><br />            <input name="email" type="text" value="" /><br />            <label>comment</label><br />            <textarea name="comment" /><br />            <input type="submit" value="send" />      </form>

now can send data javascript url. if closely @ url, able find spreadsheet's key. document key is: 10vmppmtbqsxysth20a_ku-gtj06kyxxwg-e2aatm_dm

the json feeds google spreadsheet available at:

json format: https://spreadsheets.google.com/feeds/list/your_key/od6/public/basic?alt=json

and of cause document should published before accessing json url.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -