2012-10-03 44 views
1

我查看了有關此主題的類似問題,但仍無法完全理解如何獲取由HTML創建的表單中輸入的數據。獲取由HtmlService創建的表單中提交的信息

這是我想做的事:

  • 創建表單(通過HTML)
  • 表單提交,進入使用他們的數據替換相應的佔位符鍵 在現有的谷歌文檔模板。
  • 通過電子郵件將新文檔替換爲文字給用戶。

我按照這tutorial,並能夠得到它的工作。問題是UI(Spreadsheet表單)不是我想要的。我想要一個HTML格式的表單,但無法從中正確地提取數據。

這是我使用Spreadsheet Form創建的示例腳本。

var docTemplate = "1234567890"; // Template ID 
var docName  = "FinalDocument"; // Name of the document to be created 

// When form gets submitted 
function onFormSubmit(e) { 
// Get information from form and set as variables 
    var email_address = e.values[1]; 
    var full_name = e.values[2]; 

// Get document template, copy it and save the new document's id 
    var copyId = DocsList.getFileById(docTemplate) 
       .makeCopy(docName+' for '+full_name) 
       .getId(); 
// Open the temporary document 
    var copyDoc = DocumentApp.openById(copyId); 
// Get the document's body section 
    var copyBody = copyDoc.getActiveSection(); 

// Replace place holder keys,in our google doc template 
    copyBody.replaceText('keyEmailAddress', email_address); 
    copyBody.replaceText('keyFullName', full_name); 

// Save and close the temporary document 
    copyDoc.saveAndClose(); 

// Convert temporary document to PDF by using the getAs blob conversion 
    var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); 

// Attach PDF and send the email 
    var subject = "Final Document"; 
    var body = "Here is the form for " + full_name + ""; 
    MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf}); 

// Delete temporary file 
    DocsList.getFileById(copyId).setTrashed(true); 
} 

這裏是我剛剛創建的使用HTML的新窗體。

<html> 
    <form id="myForm"> 
    <input name="fullName" id="_fullName"> 
    <input name="emailAddress" id="emailAddress"> 
    <input type="button" id="submit" value="submit" onclick = "sendData()"> 
    </form> 

    <script> 
    function sendData() { 
    google.script.run.processForm(document.getElementById("myForm")); 
    } 
    </script> 
</html> 

有人可以幫助我開始如何從使用電子表格表單轉換爲HTML表單嗎?我如何從HTML表單中提取數據?

回答

0

您可能要參考這個問題,我在Accessing object is Google App Script

問過那麼對於你的情況應該是:

// Get information from form and set as variables 
    var email_address = e.emailAddress; 
    var full_name = e.fullName; 

至於你的HTML代碼

<html> 
    <form id="myForm"> 
    <input name="fullName" id="_fullName"> 
    <input name="emailAddress" id="emailAddress"> 
    <input type="button" id="submit" value="submit" onclick = "google.script.run.processForm(this.parentNode)"> 
    </form> 
</html> 

請嘗試一下,我沒有時間測試代碼。非常抱歉。

+0

謝謝你的幫助噓。我試過你的代碼,但無法在日誌中得到結果。任何想法? (e){ function doGet(e){ var template = HtmlService.createTemplateFromFile('myForm.html'); template.action = ScriptApp.getService()。getUrl(); return template.evaluate(); } function processForm(e){ Logger.log(e.fullName); Logger.log(e.emailAddress); } – s2000coder