如何编写一个脚本,让我在单击自定义菜单时从 Google 电子表格的本地驱动器中选择和上传 CSV 文件?(我想通过脚本复制 File-->Import 命令,但在打开时呈现的新菜单中)。
这里有一个类似的问题,但答案使用了现在已弃用的方法。 脚本在 Google 电子表格中导入本地 CSV
已弃用的答案
function doGet(e) {
var app = UiApp.createApplication().setTitle("Upload CSV to Sheet");
var formContent = app.createVerticalPanel();
formContent.add(app.createFileUpload().setName('thefile'));
formContent.add(app.createSubmitButton('Start Upload'));
var form = app.createFormPanel();
form.add(formContent);
app.add(form);
// return app;
SpreadsheetApp.getActiveSpreadsheet().show(app);// show app
}
function doPost(e) {
// data returned is a blob for FileUpload widget
var fileBlob = e.parameter.thefile;
// parse the data to fill values, a two dimensional array of rows
// Assuming newlines separate rows and commas separate columns, then:
var values = []
var rows = fileBlob.contents.split('\n');
for(var r=0, max_r=rows.length; r<max_r; ++r)
values.push( rows[r].split(',') ); // rows must have the same number of columns
// Using active sheet here, but you can pull up a sheet in several other ways as well
SpreadsheetApp.getActiveSheet()
.getRange( 1, 1, values.length, values[0].length )
.setValues(values);
}