合并多个选项卡,下载为CSV

时间:2016-04-15 12:55:53

标签: google-apps-script google-sheets

我在google文件夹中有十几张纸。使用下面的脚本,我可以获得要在新电子表格中复制的每个文件的第一个选项卡。 但是现在我希望只在一个选项卡中复制所有数据,并将其作为csv文件下载到共享驱动器(而不是谷歌应用程序)。数据的格式始终相同,有时每张纸的行数不同。

此外,我想根据名称而不是数字选择标签 - 以避免将来出现错误。有任何建议如何解决这个问题?

function mergeSheets() {

/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName('name of folder').next();

/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-    apps.spreadsheet");

/* Create the new spreadsheet that you store other sheets */  
var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");

/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {

var sheet = spreadSheets.next();

/* Open the spreadsheet */
var spreadSheet = SpreadsheetApp.openById(sheet.getId());

/* Get all its sheets */
//    for(var y in spreadSheet.getSheets()) {

  /* Copy the sheet to the new merged Spread Sheet */
  spreadSheet.getSheets()[0].copyTo(newSpreadSheet); 
//    }
}      
}

2 个答案:

答案 0 :(得分:0)

function mergeSheets() {

/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName('name of folder').next();

/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-    apps.spreadsheet");

/* Create the new spreadsheet that you store other sheets */  
var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");
var newSheetTab = newSpreadSheet.getActiveSheet();

/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {

var sheet = spreadSheets.next();

/* Open the spreadsheet */
var spreadSheet = SpreadsheetApp.openById(sheet.getId());

/* Get all its sheets */
//    for(var y in spreadSheet.getSheets()) {

  /* Copy the sheet to the new merged Spread Sheet */
  var range = spreadSheet.getSheetByName('sheet name').getDataRange();

    // start after header row
    // may have to adjust these upon desired effect
    for (var i = 1; i < range.getNumRows(); i++)
    {
      newSheetTab.appendRow(range[i]);
    }

//    }
}

// then save as csv
}

请参阅this blog了解如何保存为csv。

答案 1 :(得分:0)

下面是另一种可能的解决方案,使用更快的方法(concatenate arrays)将文件夹中的所有电子表格编译成单个电子表格。

唯一的限制是所有工作表必须具有相同的列数(行数并不重要)

从这张单页中,可以很容易地将其contnet导出为CSV并将其另存为驱动器文件。 我使用您的代码作为起点,并注释了迭代每个电子表格中所有工作表的部分,就像您一样,但您当然可以取消注释for循环)。 我添加了一些注释来解释这个过程。

function mergeSheets() {
  /* Retrieve the desired folder */
  //var myFolder = DriveApp.getFoldersByName('name of folder').next();
  // or better 
  var myFolder = DriveApp.getFolderById('0B3qSFd3iikE3RDhxekVYNk5zUkk');

  /* Get all spreadsheets that resided on that folder */
  var spreadSheets = myFolder.getFilesByType(MimeType.GOOGLE_SHEETS);

  /* Create the new spreadsheet that you store other sheets */  
  var newSpreadSheet = SpreadsheetApp.create("Merged Sheets");
  var newSheetTab = newSpreadSheet.getActiveSheet();
  var values = [] ;// new array to get all sheets contents
  /* Iterate over the spreadsheets over the folder */
  var header = [];
  while(spreadSheets.hasNext()) {

    var sheet = spreadSheets.next();
    Logger.log(sheet.getName()); // log the sheet name to check
    /* Open the spreadsheet */
    var spreadSheet = SpreadsheetApp.openById(sheet.getId());

    /* Get all its sheets */
    //    for(var y in spreadSheet.getSheets()) {

    //for(var sheet in spreadSheet.getSheets()){
      var sheet = spreadSheet.getSheets()[0];// comment this line if you keep the for loop
      var range = sheet.getDataRange();
      var data = range.getValues();
      if(header.length==0){ 
        header = data.shift(); // get the header only once and remove header from data in the same time
      }else{
        var dummy = data.shift(); // remove header from data but don't keep it
      }
      values = values.concat(data); // add data to final array in each while loop 
   // }
  }
  values.unshift(header); // add the header at the array start
  Logger.log(values);//see result
  newSheetTab.getRange(1,1,values.length, values[0].length).setValues(values);// batch write to the sheet
}

// then export this single sheet as CSV see code from Google tuto
// https://developers.google.com/apps-script/articles/docslist_tutorial#section-3-saving-a-selected-range-to-a-csv-file