向.txt中添加另一行内容 [英] Add another row content to the .txt
本文介绍了向.txt中添加另一行内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只需要处理脚本即可将一行的内容导出到.txt并放一个=,但是现在我想知道如何将另一行的内容添加到.txt中,就像我将显示的那样在屏幕截图上.
I just got to work the script to export the content of a row to a .txt and put a =, but now i would like to know how i can add the content of other row to the .txt like i will show on the screenshot.
这是我目前拥有的代码,但是我无法正常工作.
This is the code that i currently have but i dont properly make to work.
function exporttotxt() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var range = sheet.getRange('G3:G' + sheet.getLastRow());
var rows = range.getValues().filter(([g]) => g.toString() != "");
var range2 = sheet.getRange('I3:I' + sheet.getLastRow());
var rows2 = range2.getValues().filter(([i]) => i.toString() != "");
var fileName="exported.txt";
var folderName="Videos";
var data = rows.splice(0);
var data2 = rows2.splice(0);
var str = data.map(function(e) {return e.join()}).join("=") + "=" + data2.map(function(e) {return e.join()}).join("\n");
var content = str;
// get list of folders with matching name
var folderList = DriveApp.getFoldersByName(folderName);
if (folderList.hasNext()) {
// found matching folder
var folder = folderList.next();
// search for files with matching name
var fileList = folder.getFilesByName(fileName);
if (fileList.hasNext()) {
// found matching file - append text
var file = fileList.next();
var combinedContent = content;
file.setContent(combinedContent);
}
else {
// file not found - create new
folder.createFile(fileName, content);
}
}
}
推荐答案
我相信您的目标如下.
- 您想通过合并列"G"的值来创建文本.和"I".
根据您的情况,如何从"G"列中检索值?和我"一个getValues
?这样,处理成本将降低,脚本变得简单一些.当这反映到您的脚本时,它如下所示.
In your situation, how about retrieving the values from the columns "G" and "I" by one getValues
? By this, the process cost will be lower and the script becomes a bit simple. When this is reflected to your script, it becomes as follows.
var range = sheet.getRange('G3:G' + sheet.getLastRow());
var rows = range.getValues().filter(([g]) => g.toString() != "");
var range2 = sheet.getRange('I3:I' + sheet.getLastRow());
var rows2 = range2.getValues().filter(([i]) => i.toString() != "");
var fileName="exported.txt";
var folderName="Videos";
var data = rows.splice(0);
var data2 = rows2.splice(0);
var str = data.map(function(e) {return e.join()}).join("=") + "=" + data2.map(function(e) {return e.join()}).join("\n");
var content = str;
收件人:
var range = sheet.getRange('G3:I' + sheet.getLastRow());
var rows = range.getValues().filter(([g, _, i]) => g.toString() != "" && i.toString() != "");
var fileName="exported.txt";
var folderName="Videos";
var data = rows.splice(0);
var str = data.map(([g, _, i]) => `${g}=${i}`).join("\n");
var content = str;
这篇关于向.txt中添加另一行内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文