Export Google spreadsheet to text file using script

被刻印的时光 ゝ 提交于 2019-12-07 18:06:51

问题


I have a Google spreadsheet file containing just one sheet with one column. Everytime it is changed, it should be saved as a simple text (*.txt) file on my Google drive. So I would use the "on edit" trigger to run the script.

I found a few scripts that supposedly once did exactly what I need, but they seem to use outdated references, like this one which was posted two years ago. So my question is, how to export a sheet to text file now, in 2016?


回答1:


Here is a simple export function that works for me: it exports the current sheet as tab-delimited text file, whose name includes the date of creation.

You can set it to be triggered on edit using Resources > Current Project's Triggers in the Script Editor.

function export() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();
  var text = values.map(function (a) {return a.join('\t');}).join('\n');
  DriveApp.createFile('Data as of ' + new Date(), text);
}

Depending on your editor/OS, you may want join('\r\n') instead of join('\n') here.



来源:https://stackoverflow.com/questions/36756045/export-google-spreadsheet-to-text-file-using-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!