2016-11-10 165 views
0

有什麼辦法如何使用谷歌牀單直接從客戶端JavaScript寫入和讀取數據在Web瀏覽器?REST API谷歌表格 - 客戶端使用JS

數據安全性在這裏沒有問題。我只需要一些小的免費數據庫用於沒有Web服務器的單頁網頁應用程序。

+1

你檢查https://firebase.google.com/ – oak

+0

不,但我現在:) –

回答

1

是的,有一種方法。有時我使用Google表格來保存表單數據。所以我不會給你完整的解決方案,但它可能是一個很好的起點。

  • 在Google表格中創建或打開電子表格。
  • 選擇菜單項Tools> Script editor。如果出現歡迎屏幕,請單擊左側的空白項目開始一個新項目。
  • 刪除腳本編輯器中的任何代碼。而簡單地複製和粘貼代碼到腳本編輯器:

// 1. Enter sheet name where data is to be written below 
 
     var SHEET_NAME = "Sheet1"; 
 
      
 
// 2. Run > setup 
 
// 
 
// 3. Publish > Deploy as web app 
 
// - enter Project Version name and click 'Save New Version' 
 
// - set security level and enable service (most likely execute as 'me' and access 'anyone, even anonymously) 
 
// 
 
// 4. Copy the 'Current web app URL' and post this in your form/script action 
 
// 
 
// 5. Insert column names on your destination sheet matching the parameter names of the data you are passing in (exactly matching case) 
 
    
 
var SCRIPT_PROP = PropertiesService.getScriptProperties(); // new property service 
 
    
 
// If you don't want to expose either GET or POST methods you can comment out the appropriate function 
 
function doGet(e){ 
 
    return handleResponse(e); 
 
} 
 
    
 
function doPost(e){ 
 
    return handleResponse(e); 
 
} 
 
    
 
function handleResponse(e) { 
 
    // shortly after my original solution Google announced the LockService[1] 
 
    // this prevents concurrent access overwritting data 
 
    // [1] http://googleappsdeveloper.blogspot.co.uk/2011/10/concurrency-and-google-apps-script.html 
 
    // we want a public lock, one that locks for all invocations 
 
    var lock = LockService.getPublicLock(); 
 
    lock.waitLock(30000); // wait 30 seconds before conceding defeat. 
 
    
 
    try { 
 
    // next set where we write the data - you could write to multiple/alternate destinations 
 
    var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key")); 
 
    var sheet = doc.getSheetByName(SHEET_NAME); 
 
     
 
    // we'll assume header is in row 1 but you can override with 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; // get next row 
 
    var row = []; 
 
    // loop through the header columns 
 
    for (i in headers){ 
 
     if (headers[i] == "Timestamp"){ // special case if you include a 'Timestamp' column 
 
     row.push(new Date()); 
 
     } else { // else use header name to get data 
 
     row.push(e.parameter[headers[i]]); 
 
     } 
 
    } 
 
    // more efficient to set values as [][] array than 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 this 
 
    return ContentService 
 
      .createTextOutput(JSON.stringify({"result":"error", "error": e})) 
 
      .setMimeType(ContentService.MimeType.JSON); 
 
    } finally { //release lock 
 
    lock.releaseLock(); 
 
    } 
 
} 
 
    
 
function setup() { 
 
    var doc = SpreadsheetApp.getActiveSpreadsheet(); 
 
    SCRIPT_PROP.setProperty("key", doc.getId()); 
 
}

  • 發佈>部署爲Web應用程序。
  • 選擇您的安全選項。然後按「部署」
  • 現在會爲您生成URL,在我的情況下,此URL是:https://script.google.com/macros/s/AKfycbyRs2qfvz5-qmhyFPVmpm0hayjcofsDIcnypkb_Zz0fDzriMWAb/exe
  • 在使用您的鏈接之前,請不要忘記運行>設置。
  • 最終,您可以通過將表單數據發送到 您的URL將數據發送到您的工作表。你的工作表的第一行應該包含你正在計劃保存的字段名稱。在這種情況下,寫入A1:Name,在B1:Mail和C1-> Comment中。

<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>

現在,您可以用JavaScript將數據發送到您的網址。如果仔細查看網址,您將能夠找到電子表格的關鍵字。我的文檔的關鍵是:10vmpPMTBqsXYsTH20a_kU-GTJ06KYxXWg-E2aAtm_dM

的JSON用於任何谷歌電子表格供稿,請訪問:

JSON格式: https://spreadsheets.google.com/feeds/list/YOUR_KEY/od6/public/basic?alt=json

並可使您的文檔應該訪問JSON URL之前公佈。

+0

非常感謝你的答案好的答案Antanas。 現在我必須將這個解決方案與解決方案「櫟木」提出的 - Firebase進行比較。我認爲也是很好的解決方案。 –

相關問題