简体   繁体   中英

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.

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.

  • You want to merge the CSV files in one CSV file.
    • The CSV files are in a specific folder.
  • You want to create the merged CSV file in the specific folder.
  • When the CSV data is merged, you want to delete the rows which has Titel in the column "A".
  • You want to achieve this using Google Apps Script.

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?

Flow:

  1. Retrieve each CSV file from the specific folder.
  2. Parse the file as the CSV data.
  3. From the CSV data, delete the rows which has Titel in the column "A".
  4. Put the CSV data to an array.
  5. After all CSV files were processed from above 2 to 4, the array including the CSV data is exported as a CSV file to the specific folder.

When above flow is reflected to the script, it becomes as follows.

Modified script:

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);
}

Note:

  • 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);

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Edit:

Pattern 1:

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();
  • Below modified script leaves only the 1st row at the 1st CSV file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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