[英]Save spreadsheet as a csv file in given folder
I need a spreadsheet from my root folder to be saved as a csv file in a specific given folder.我需要将根文件夹中的电子表格保存为特定给定文件夹中的 csv 文件。
With the help of members, I was able to merge multiple CSV files into a single sheet and delete the same contents from multiple rows, now I want to save this sheet from my root folder as a CSV file in my given folder.在成员的帮助下,我能够将多个 CSV 文件合并到一个工作表中并从多行中删除相同的内容,现在我想将此工作表从我的根文件夹中保存为我给定文件夹中的 CSV 文件。
/* Retrieve the desired folder */
var myFolder = DriveApp.getFolderById('1niUw2-');
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType(MimeType.CSV);
/* Create the new spreadsheet that you store other sheets */
var newSpreadSheet = SpreadsheetApp.create("Merge Sheets");
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
var file = DriveApp.getFileById(sheet.getId());
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString());
var newSheet = newSpreadSheet.getActiveSheet();
for (var i=0;i<csvData.length;i++) {
newSheet.appendRow(csvData[i]);
}
var data = newSheet.getDataRange();
var values = data.getValues();
for (var i=values.length -1; i >=1; i--){
if (values[i][0] == "Titel"){
newSheet.deleteRow(i+1);
}
}
myFolder.createFile('MERGED',newSheet,MimeType.CSV);
} }
I need this edited spreadsheet file " Merged sheet " from my root folder into my given folder as a csv file.我需要这个编辑过的电子表格文件“Merged sheet”从我的根文件夹到我给定的文件夹中作为 csv 文件。
Titel
in the column "A".Titel
的行。 If my understanding is correct, how about this modification?如果我的理解是正确的,那么这个修改呢? Please think of this as just one of several answers.
请认为这只是几个答案之一。
In your script, each CSV data is put to the new Spreadsheet using appendRow()
, and then, when the value of column "A" is Titel
, the row is deleted.在您的脚本中,使用
appendRow()
将每个 CSV 数据放入新的电子表格中,然后,当“A”列的值为Titel
时,该行被删除。 In this case, I thought that the process cost might become high.在这种情况下,我认为工艺成本可能会变高。 So how about the following flow?
那么下面的流程呢?
Titel
in the column "A".Titel
的行。 When above flow is reflected to the script, it becomes as follows.当上述流程反映到脚本时,它变成如下。
function myFunction() {
var myFolder = DriveApp.getFolderById('###'); // Please set the folder ID including the CSV files.
var csvFiles = myFolder.getFilesByType(MimeType.CSV);
var data = [];
while (csvFiles.hasNext()) {
var csvFile = csvFiles.next();
var file = DriveApp.getFileById(csvFile.getId());
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()).filter(function(row) {return row[0] != "Titel"});
Array.prototype.push.apply(data, csvData);
}
myFolder.createFile('MERGED', data.map(function(row) {return row.join(",")}).join("\n"), MimeType.CSV);
}
If you want to create the merged CSV data as a Spreadsheet, please put the following script to the last line of the function of myFunction
.如果要将合并的 CSV 数据创建为电子表格,请将以下脚本放在
myFunction
的 function 的最后一行。
SpreadsheetApp.create("Merge Sheets").getSheets()[0].getRange(1, 1, data.length, data[0].length).setValues(data);
If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
If you want to delete the 1st row of each CSV data, how about the following modification?如果要删除每个CSV数据的第一行,下面的修改如何? Please modify my above modified script as follows.
请修改我上面修改的脚本如下。
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()).filter(function(row) {return row[0];= "Titel"});
To:
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); csvData.shift();
or或者
var csvData = Utilities.parseCsv(file.getBlob().getDataAsString()); if (data.length > 0) csvData.shift();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.