关闭模板电子表格并激活重复的电子表格

时间:2016-10-22 00:39:49

标签: google-apps-script google-sheets

我是App Script的新手,请原谅我的无知,如果这有一个简单的解决方案。

我正在使用模板电子表格,我想执行以下操作:

1 - 当使用今天的日期打开创建副本时 2 - 在步骤1之后,然后关闭模板并将步骤1中的副本作为活动电子表格。

我使用以下代码完成了第1步:

function saveAsSpreadsheet(){ 

var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxfolderIDxxx"); 
var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 1;
var curr_year = d.getFullYear();
var theDate = curr_date + "-" + curr_month + "-" + curr_year;
var newFileName = "EoD " + theDate;

DriveApp.getFileById(sheet.getId()).makeCopy(newFileName, destFolder); 
}

但是我遇到了第2步的问题。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

我有一个类似功能的功能,但它通过创建一个全新的文件来实现。这是我的剧本。我当前的脚本抓取文件夹中的gsheet文件,将它们组合到一个新的gsheet文件中,并将一个模板添加到工作表1中。

   /* Head Master Info >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */

/* Top Level Google Drive Folder ID */
var TopDriveFolderID = "0B2rN5b8fW77ldXZXOXFLZGlSamc";
var BaseNumberOfFilesInFolder = 4;
var NewSSAnalyzerSheetId = "1VyL8XL1x41H6yetcrwfUhVcch-DUsngY4-bHCbo-_xM";

/* End Head Master Info >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

/* Body >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

/* Functions to iterate through all sub folders */
function listFolders() {
  var parentFolder = DriveApp.getFolderById(TopDriveFolderID);
  var childFolders = parentFolder.getFolders();
  while(childFolders.hasNext()) {
    var child = childFolders.next();
    //    Logger.log(child.getName() + " |Drive_ID: " + child.getId());
    var folderId = child.getId();
    // **the folderId Variable is also the folder ID,
    //    hence they are used interchangeably **

    /* SpreadSheet Combiner >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/    

    /* Name of combined Spreadsheet*/
    var newSpreadSheetName = DriveApp.getFolderById(folderId).getName();

    /* Retrieve the desired folder */
    var myFolder = DriveApp.getFolderById(folderId);

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

    /* Check if a file needs to be created or if one already exists*/  
    var theFolder = DriveApp.getFolderById(folderId);
    var files = theFolder.getFiles();
    var fname = theFolder.getName();

    var cnt = 0;
    var file;

    /* Check the quantity of files in a folder to see if you need to combine the sheets and create a file*/  
    while (files.hasNext()) {
      cnt++;
      file = files.next();
      //Logger.log(file.getName());
      if (cnt > BaseNumberOfFilesInFolder) {
        Logger.log("File already exists")
        break;
      } 
      ;
    };

    // If a file needs to be created this checks it and creates it
    Logger.log(cnt + " is the number of files '"+ newSpreadSheetName+ "' has.");
    if (cnt > BaseNumberOfFilesInFolder){     
      Logger.log("No file needs to be created")
    }else{

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


      /* 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()[y].copyTo(newSpreadSheet);


          /* In order to move the file to the folder we want and because 
          google considers the SpreadSheet a Google Spreadsheet
          instead of a file, we have to convert the SpreadSheet to a file in
          order to move it.Thats what the next 2 lines of code do.*/
          var getNewSSid = newSpreadSheet.getId();
          var SStoGFile = DriveApp.getFileById(getNewSSid);
          /* Actually moving the file*/
          DriveApp.getFolderById(folderId).addFile(SStoGFile);
          /* Deleting the duplicate file that was created in the process*/
          var rootFolder = DriveApp.getRootFolder();
          DriveApp.getRootFolder().removeFile(SStoGFile);

        }
      } 

      /*Code to add our base analysis template to the new combined template that was created;*/
      //Grabs the template
      var sss = SpreadsheetApp.openById(NewSSAnalyzerSheetId); //replace with source ID
      var ss = sss.getSheetByName('Analysis Tab'); //replace with source Sheet tab name
      var range = ss.getRange('A1:E6'); //assign the range you want to copy
      var data = range.getValues();

      //Copies the template info into the current newly created sheet
      var tssId = newSpreadSheet.getId();
      var tss = SpreadsheetApp.openById(tssId); //replace with destination ID
      var ts = tss.getSheetByName('Sheet1'); //replace with destination Sheet tab name
      ts.getRange(1,1,6,5).setValues(data); //you will need to define the size of the copied data see getRange()


      Logger.log("New File was created with the name - " + newSpreadSheetName)
    }
  };


  /* End SpreadSheet Combiner >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/       


  getSubFolders(child); 

}


 /* Necessary Function to help the above loop through the sub folders */
function getSubFolders(parent) {
  parent = parent.getId();
  var childFolder = DriveApp.getFolderById(parent).getFolders();
  while(childFolder.hasNext()) {
    var child = childFolder.next();
    Logger.log(child.getName());
    getSubFolders(child);
  }
  return;
}

/* End iterate through Sub Folders */