在谷歌电子表格脚本导入本地CSV(Script import local CSV in Google

2019-07-30 04:22发布

如何导入本地硬盘驱动器一个CSV文件中的谷歌电子表格文档我自己吗? (我想通过脚本来复制文件 - >导入命令)

Answer 1:

这类似于错误的答案与文档列表,但不是使用已超出DocsList你可以直接得到的数据出来团块,分析它,然后将其导入到电子表格中。 我只给出一个简要介绍:

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);
}


文章来源: Script import local CSV in Google Spreadsheet