谷歌脚本:如何脚本自动导入从我的驱动器的TXT到电子表格? [英] Google Script: How to script an automatic import from a txt in my drive into spreadsheet?

查看:189
本文介绍了谷歌脚本:如何脚本自动导入从我的驱动器的TXT到电子表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前从未使用Javascript,并且我一直在努力尝试这样做,但没有运气,我找不到任何以前的人尝试。
我想直接从我的驱动器中的txt文档复制文本数据,可以手动完成这项工作,但我希望每天自动完成。

文本文档

  Boxes Made,3 Target Percentage,34 Hourly Rate,2  

b
$ b

如果我将这些设置导入到电子表格中,这些设置就非常完美;

导入设置



它以这种方式导入;

汇入后



现在我需要尝试自动执行此操作,以便脚本自动导入它。
我目前使用的脚本无法使用,请帮助。



当前脚本;

  function AutoImporter(Source){var Source = DriveApp.getFilesByName('DailyData.txt'); var TextContents = Source.copyText(); var Target = SpreadsheetApp.getActiveSheet(); Target.appendText(TextContents [1]); }  

- 编辑 b $ b

有些人给我发了一个脚本,看起来更接近但仍然无效;

  function autoCSV(){var ss = SpreadsheetApp.getActiveSpreadsheet(); var s = ss.getActiveSheet(); var r = s.getActiveCell(); var id =DailyData.txt; //<<<<<输入文本文件的ID var f3 = DriveApp.getFileById(id); var lst1 = f3.getBlob()。getDataAsString()。split('\ n')。map(function(x){return x.split(',')}); var ncols = 1,i,lst2 = [];对于(i = 0; i  ncols)ncols = lst1 [i] .length; for(i in lst1)lst1 [i] = lst1 [i] .concat(lst2.slice(0,lst2.length-lst1 [i] .length)); s.getRange(),r.getColumn(),lst1.length,ncols).setValues(lst1);}  

解决方案

您可以通过Google Drive从Google Drive中读取文本文件:

 'use strict'; //<  - 始终使用严格模式。 

函数foo(){

var fileName ='DailyData.txt';
var files = DriveApp.getFilesByName(fileName);
if(!files.hasNext()){
抛出新错误('没有名称的文件:'+ fileName);
}
//我们只用这样的名字取所有文件中的第一个文件。
var file = files.next();
var text = file.getBlob()。getDataAsString('utf8');
Logger.log(text);
//现在你必须解析文件。

$ b $ / code>

文件:


  1. DriveApp。 getFilesByName 返回文件的集合。

  2. 返回 Blob

  3. Blob.getDataAsString 返回字符串


I've never used Javascript before and i've been trying for ages to do this but with no luck, and I can't find any previous people trying. I want to copy the text data straight from this txt document in my drive, it is possible to do this fine manually but I want it to be done daily automatically instead.

The text document;

Boxes Made,3  
Target Percentage,34  
Hourly Rate,2

If I import this into a spreadsheet with these settings its perfect;

Import Settings

And it imports like this;

After Import

Now I need to try and automate this so that a script imports it automatically. The script I have so far doesn't work, please help.

Current script;

 function AutoImporter (Source)
{
  var Source = DriveApp.getFilesByName('DailyData.txt');
  var TextContents = Source.copyText();
  var Target = SpreadsheetApp.getActiveSheet();
    
  Target.appendText(TextContents[1]);  
  }

--edit

Some guy just sent me a script that seems closer but still didn't work;

function autoCSV() {
  var ss=SpreadsheetApp.getActiveSpreadsheet();
  var s=ss.getActiveSheet();
  var r=s.getActiveCell();
  var id="DailyData.txt";//<<<<<enter the ID of the text file
  var f3=DriveApp.getFileById(id);
  var lst1=f3.getBlob().getDataAsString().split('\n').map(function(x) {return x.split(',')});
  var ncols=1,i,lst2=[];
  for (i in lst1) {if (lst1[i].length>ncols) ncols=lst1[i].length;}
  for (i=0;i<ncols;i++) lst2.push('');
  for (i in lst1) lst1[i]=lst1[i].concat(lst2.slice(0,lst2.length-lst1[i].length));

  s.getRange(r.getRow(), r.getColumn(), lst1.length, ncols).setValues(lst1);
}

解决方案

You may read text file from Google Drive this way:

'use strict'; // <- Always use strict mode.

function foo() {

  var fileName = 'DailyData.txt';
  var files = DriveApp.getFilesByName(fileName);
  if (!files.hasNext()) {
    throw new Error('No file with name:' + fileName);
  }
  // We take only the first file among all files with such name.
  var file = files.next();
  var text = file.getBlob().getDataAsString('utf8');
  Logger.log(text);
  // Now you have to parse the file.

}

Documentation:

  1. DriveApp.getFilesByName returns collection of Files.
  2. File.getBlob returns Blob.
  3. Blob.getDataAsString returns String.

这篇关于谷歌脚本:如何脚本自动导入从我的驱动器的TXT到电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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