【问题标题】:Upload a textfile using script in Google spreadsheets and write the contents of the textfile into a sheet in the spreadsheet使用 Google 电子表格中的脚本上传文本文件,并将文本文件的内容写入电子表格中的工作表
【发布时间】:2012-11-21 16:30:04
【问题描述】:
使用通过单击 Google 电子表格上的按钮调用的自定义函数,我想将文本文件上传到特定文件夹,然后将内容放入电子表格的特定工作表中?
我知道如何创建一个按钮并在点击它时调用一个自定义函数。现在通过我的自定义函数,我想:
- 要求用户浏览到文本文件位置,然后选择文件并上传。
- 将其上传到特定位置,并将文件名更改为我想要的特定名称。
- 读取文本文件的内容并将它们放入电子表格的工作表中。
谢谢!
【问题讨论】:
标签:
upload
google-apps-script
text-files
google-sheets
google-spreadsheet-api
【解决方案1】:
创建一个按钮,并为该按钮分配一个宏(功能),或者您也可以在 Google 电子表格中轻松创建一个新菜单项。新菜单项将使用自定义函数以编程方式实现,并使用电子表格的 onOpen 触发器执行。
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Text File Upload", functionName: "text_file_upload"},
{name: "Change Name", functionName: "change_name"},
{name: "TextFile to S.sheet", functionName: "t_file_ssheet"} ];
ss.addMenu("Text Files", menuEntries);
} //this onOpen function will add a new menu to your spreadsheet called "Text Files" with 3 submenu items you can use to work with your text files.
function text_file_upload() {
Browser.msgBox("You are about to upload new text file");
//insert code here to get textfile location/Url and Textfile Data, this can be stored in array
}
function change_name() {
var newName = Browser.inputBox("Enter the new name for the text file:");
//insert code here to assign new file name to the text file
}
function t_file_ssheet(){
//insert code here to copy data from your textfile to a sheet in your spreadsheet
}
Tutorial: Interacting With Your Docs List 这里有一个非常好的教程,涵盖了您正在寻找的大部分编码。