Reputation: 27
I need a spreadsheet from my root folder to be saved as a csv file in a specific given folder.
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.
/* 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.
Upvotes: 1
Views: 317
Reputation: 201378
Titel
in the column "A".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. In this case, I thought that the process cost might become high. So how about the following flow?
Titel
in the column "A".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
.
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? Please modify my above modified script as follows.
From: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();
Upvotes: 2