在Google Spreadsheet中脚本导入本地CSV [英] Script import local CSV in Google Spreadsheet

查看:145
本文介绍了在Google Spreadsheet中脚本导入本地CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从我自己的Google Spreadsheet文档中的本地硬盘中导入CSV文件? (我想通过脚本复制文件 - >导入命令)

解决方案

这与DocList的错误答案类似,但不是使用DocsList,而是直接从blob中获取数据,解析数据并将其导入到电子表格中。我只给出一个简要概述:
$ b $ pre $ 函数doGet(e){
var app = UiApp.createApplication( ).setTitle(将CSV上传到工作表);
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);
//返回应用程序;
SpreadsheetApp.getActiveSpreadsheet()。show(app); //显示应用程序
}

函数doPost(e){
//返回的数据是blob FileUpload小部件
var fileBlob = e.parameter.thefile;

//解析数据以填充值,一个二维行数组
//假设换行符将行和逗号分隔开,那么:
var values = []
var rows = fileBlob.contents.split('\\\
'); (var r = 0,max_r = rows.length; r values.push(rows [r] .split(','));
//行必须具有相同数量的列

//在此处使用活动工作表,但也可以通过其他几种方式提取工作表
SpreadsheetApp.getActiveSheet()
.getRange(1,1,values.length,values [0] .length)
.setValues(values);
}


How do I import a CSV file from local hard drive in a Google Spreadsheet document I own? (I want to replicate via script the File-->Import command)

解决方案

This is similar to wrong answer with the DocList, but instead of using DocsList you can get the data directly out of the blob, parse it and then import it into a spreadsheet. I've only give a brief outline:

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

这篇关于在Google Spreadsheet中脚本导入本地CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆