简体   繁体   English

将电子表格另存为给定文件夹中的 csv 文件

[英]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 文件。

  • You want to merge the CSV files in one CSV file.您希望将 CSV 文件合并到一个 CSV 文件中。
    • The CSV files are in a specific folder. CSV 文件位于特定文件夹中。
  • You want to create the merged CSV file in the specific folder.您想在特定文件夹中创建合并的 CSV 文件。
  • When the CSV data is merged, you want to delete the rows which has Titel in the column "A".合并 CSV 数据时,要删除“A”列中具有Titel的行。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本实现此目的。

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?那么下面的流程呢?

Flow:流动:

  1. Retrieve each CSV file from the specific folder.从特定文件夹中检索每个 CSV 文件。
  2. Parse the file as the CSV data.将文件解析为 CSV 数据。
  3. From the CSV data, delete the rows which has Titel in the column "A".从 CSV 数据中,删除“A”列中具有Titel的行。
  4. Put the CSV data to an array.将 CSV 数据放入数组。
  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.在从上述 2 到 4 处理完所有 CSV 文件后,包含 CSV 数据的数组将作为 CSV 文件导出到特定文件夹。

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 .如果要将合并的 CSV 数据创建为电子表格,请将以下脚本放在myFunction的 function 的最后一行。

     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?如果要删除每个CSV数据的第一行,下面的修改如何? 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.下面修改后的脚本仅在第一个 CSV 文件中留下第一行。

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

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