有什麼辦法如何使用谷歌牀單直接從客戶端JavaScript寫入和讀取數據在Web瀏覽器?REST API谷歌表格 - 客戶端使用JS
數據安全性在這裏沒有問題。我只需要一些小的免費數據庫用於沒有Web服務器的單頁網頁應用程序。
有什麼辦法如何使用谷歌牀單直接從客戶端JavaScript寫入和讀取數據在Web瀏覽器?REST API谷歌表格 - 客戶端使用JS
數據安全性在這裏沒有問題。我只需要一些小的免費數據庫用於沒有Web服務器的單頁網頁應用程序。
是的,有一種方法。有時我使用Google表格來保存表單數據。所以我不會給你完整的解決方案,但它可能是一個很好的起點。
// 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());
}
<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之前公佈。
非常感謝你的答案好的答案Antanas。 現在我必須將這個解決方案與解決方案「櫟木」提出的 - Firebase進行比較。我認爲也是很好的解決方案。 –
你檢查https://firebase.google.com/ – oak
不,但我現在:) –