2014-09-25 46 views
0

嗨下面的腳本允許我上傳文件到谷歌驅動器,但我似乎無法得到輸入數據輸入'名稱'和'日期'進入到一個電子表格。谷歌腳本表格與上傳,如何連接到電子表格

誰能幫我創建或鏈接電子表格中的數據來填充進去。

感謝

code.gs

function doGet(e) { 
    return HtmlService.createHtmlOutputFromFile('form.html'); 
} 

function uploadFiles(form) { 

    try { 

    var dropbox = "Operation Overview"; 
    var folder, folders = DriveApp.getFoldersByName(dropbox); 

    if (folders.hasNext()) { 
     folder = folder.next(); 
    } else { 
     folder = DriveApp.createFolder(dropbox); 
    } 

    var blob = form.myFile;  
    var file = folders.createFile(blob); 
    file.setDescription("Uploaded by "+ form.myName); 

    return "File uploaded successfully "; 

    } catch (error) { 

    return error.toString(); 
    } 

} 

form.htm升

<form id="myForm"> 
    <input type="text" name="myName" placeholder="Your name."> 
    <input type="text" name="myDate" placeholder="Date."> 
    <input type="file" name="myFile"> 
    <input type="submit" value="Upload File" 
      onclick="this.value='Uploading..'; 
        google.script.run.withSuccessHandler(fileUploaded) 
        .uploadFiles(this.parentNode); 
        return false;"> 
</form> 

<div id="output"></div> 

<script> 
    function fileUploaded(status) { 
     document.getElementById('myForm').style.display = 'none'; 
     document.getElementById('output').innerHTML = status; 
    } 
</script> 

<style> 
input { display:block; margin: 20px; } 
</style> 
+0

這並不直接相關,只是爲了幫助更全面地理解代碼。你在'doGet(e)'中有參數'e'。除非將URL搜索字符串參數傳遞到您的應用程序,否則不需要在那裏放入'e'。它不會在你的代碼運行方式上產生任何影響,但它很好地知道它的用途。 – 2014-09-26 15:52:35

回答

0

您需要使用電子表格服務

Google Documentation - Apps Script Spreadsheet Service

您可以使用附加行:

Append Rows

var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheets()[0]; 

// Appends a new row with 2 columns to the bottom of the 
// spreadsheet containing the values in the array 
sheet.appendRow(["Jackson Jones", "[email protected]"]); 

上面的例子使用s的getActiveSpreadsheet()方法。你將不會使用它。您需要對文件的引用。

您可以通過id或url打開電子表格文件。

Google Documentation - Open a Spreadsheet by ID

// The code below opens a spreadsheet using its ID and logs the name for it. 
// Note that the spreadsheet is NOT physically opened on the client side. 
// It is opened on the server only (for modification by the script). 
var ss = SpreadsheetApp.openById("abc1234567"); 
Logger.log(ss.getName()); 

通知的Logger.log()聲明。要查看日誌,請在Apps腳本代碼編輯器中使用查看菜單。

重要的是要明白,SpreadsheetApp.openById()返回CLASS。它返回電子表格CLASS

Google Documentation - Spreadsheet Class

一旦你的電子表格類,那麼你可以提供給電子表格類下一步和使用方法。

var ss = SpreadsheetApp.openById("abc1234567"); 
Logger.log(ss.getName()); 
ss.appendRow(["Jackson Jones", "[email protected]"]); 

在上面的例子中,數據是硬編碼的,但是你需要變量而不是雙引號中的常量值。

因此,您需要從表單對象中獲取數據,並將其轉換爲變量或直接引用。

ss.appendRow([form.myName, "[email protected]"]); 

我沒有檢查你的代碼,並form.myName做回我進入名稱字段的名稱。

相關問題