2015-12-26 54 views
2

我發現這種方法存儲數據從HTML [鏈接] Add basic data to a Google Spreadsheet via a simple HTML Form,但這種方法發送數據不按順序。我想送的指令和數據只有一些變量 我有這個HTML文件只發送一些變量到電子表格從一個HTML表格

<!DOCTYPE html> 
<html> 
    <head> 
    <base target="_top"> 
    </head> 
    <body> 
    <form id="form" method="get" action="https://script.google.com/macros/s/AKfycbzhVAFaP7tFTbSzgeO0y9rUBl6ptVRCbqpS9wi12yFmoboYjw/exec" accept-charset="UTF-8"> 
    Name and age! 
    <input type=text name=name id=name> 
    <input type="text" name="direccion" placeholder="Direccion completa"> 
    <input type=number name=age id=age> 
    <input type="Apellido" name="Apellido" id="Apellido"> 
    <input type=submit onclick=google.script.run.addProduct()> 
</form>  
</body> 
</html> 

而且我在我的電子表格這種code.gs

function doGet(e){ 
    var vals=[]; 
    vals.push(new Date()); 
    for(var i in e.parameter){ 
    vals.push(e.parameter[i]); 
    } 
    SpreadsheetApp.openById("1KYR7SefLy5P-JP3LqdKsRRd_bkwTHFGFQQ0rnD3JfUM").appendRow(vals); 
    return ContentService.createTextOutput("Información enviada correctamente"); 
} 

有一些方法來發送變量訂購?

回答

0

當表單標籤中的操作提交表單時,表單元素將被提交給URL。表單元素是一個對象。一個對象將改變元素的順序。數組保證數據的順序,對象不保證數據在對象中的順序。

但是,您可以通過它的鍵名來引用對象中的每個元素,在這種情況下,它將是name屬性。

function doGet(e){ 

    var vals=[]; 
    vals.push(new Date()); 
    vals.push(e.parameter.name); 
    vals.push(e.parameter.direccion); 
    vals.push(e.parameter.age); 
    vals.push(e.parameter.Apellido); 

    Logger.log('vals: ' + vals); 
    SpreadsheetApp.openById("").appendRow(vals); 
    //return ContentService.createTextOutput(""); 
}; 

確保訂單的唯一方法是在數據發送前設置訂單,或者在檢索數據後設置訂單。上面的例子設置了數據處理的順序。

發送一些輸入值的唯一方法是在發送表單之前處理表單。顯然,你可以省略服務器端的一些值。如果您打算在表單標籤中使用該策略,那麼您不需要在提交按鈕中提供以下內容:

onclick=google.script.run.addProduct() 

+0

你真棒:)謝謝,這解決了我的問題 –

相關問題