2014-09-25 111 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做回我进入名称字段的名称。

相关问题