简体   繁体   English

如何在谷歌应用程序脚本中输出文本文件

[英]How to output a text file in google apps script

I am using Google scripts to try and output 2 .txt files by reading in the information from Sheet 1.我正在使用 Google 脚本通过读取 Sheet 1 中的信息来尝试输出 2 个 .txt 文件。

I want my files to look like below one for just titles and the other for footnotes:我希望我的文件看起来像下面一个只是标题,另一个是脚注:

Therefore the '|'因此'|' character in the footnote column needs to split onto a new line with the same program name listed as above.脚注列中的字符需要拆分到与上面列出的程序名称相同的新行。

The attached sheet is a basic example, but I need guidance which would also work if the footnote column had multiple '|'随附的表格是一个基本示例,但我需要指导,如果脚注列有多个“|”,该指导也适用characters.人物。

Any help is appreciated.任何帮助表示赞赏。

在此处输入图片说明

在此处输入图片说明

Solution解决方案

Use the split() JS String method to obtain your strings in a single array.使用split() JS String 方法获取单个数组中的字符串。

When obtaining the values from a Spreadsheet the Apps Script methods will arrange them in multidimensional Arrays using this fashon: sheet[rows][columns] .当从电子表格中获取值时,Apps 脚本方法将使用以下方式将它们排列在多维数组中: sheet[rows][columns]

In this example I will use the 3rd column, but you can of course adapt this method to whatever column serves you best:在本例中,我将使用第 3 列,但您当然可以将此方法应用于最适合您的任何列:

function splitter() {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var thirdColumnValues = ss.getDataRange().getValues().flatMap(row => row[2].split('|'));
  var text = thirdColumnValues.join("\n"); //Just concatenate the values with the "return" symbol
}

Reference参考

getValues() 获取值()

JS String JS字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM