我需要使用脚本将google电子表格的副本保存到特定目录

时间:2014-08-01 16:14:07

标签: google-apps-script google-drive-api google-sheets google-docs

我们目前有一个夜间平衡过程,其中我们打开Excel模板电子表格,输入正确的处理日期,然后单击按钮,导致VB脚本启动并创建具有相应名称的电子表格的新副本(例如,“BAL 080114”)在相应的文件夹中打开它,然后操作员关闭模板,并继续在新副本中工作。

文件夹结构为:

Drive
--->Ops Room
------->Procedural Sheets
----------->Night Shift
--------------->Balancing
------------------->2014
----------------------->01-2014
...
----------------------->12-2014

我们正在尝试将此转换为Google docs电子表格,而且大多数情况下它正在运行。但我找不到允许某人打开模板(存储在Balancing中),运行“开始新的一天”脚本,并让脚本在正确的子子文件夹中创建文件的方法。也就是说,对于2014年1月8日,该文件应存储在Balancing / 2014 / 08-2014中,作为Bal 080114。

这就是我到目前为止:

function startNewDay() {
  // This code makes a copy of the current spreadsheet and names it appropriately
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  // The file name is created and stored on sheet "Set Date" in cell B5
  var fname = ss.getSheetByName("Set Date").getRange("B5").getValue();
  var folderYear = Utilities.formatDate(new Date(), "GMT-6", "yyyy"); // top-level folder is by year in yyyy format
  var folderMonth = Utilities.formatDate(new Date(), "GMT-6", "MM-yyyy"); // folder name is in mm-yyyy format
  //the above is probably overkill, but I'll work on efficiency once I get it working at all :
  //Everything works up to this point...

  //This is where I start running into problems...
  //The Master Copy SSID is <redacted>
  var SSID = '<redacted>'
  var folder = DocsList.getFolder(folderYear + "/" + folderMonth); 
  var backup = DocsList.getFileById(SSID).makeCopy(fname);
  backup.addToFolder(folder); //This line will move the file to the appropriate folder
  backup.removeFromFolder(DocsList.getRootFolder()); //This line is needed to remove the File from the Root
}

我从另一个具有类似属性的StackOverFlow答案中借用了备份。*东西,但我的版本没有创建该文件。

我在Drive中尝试做什么?或者我只需让操作员创建一个副本然后手动移动它?

我为任何脚本无知道歉 - 我本周刚刚开始学习Google脚本,而且我在以前的VB体验中找不到共同点。

提前感谢您的帮助。

詹姆斯

1 个答案:

答案 0 :(得分:0)

我无法评论,所以我无法添加已经存在的内容。

文件夹是&#34;手动&#34;创建是由正确的人使用正确的权限创建的?共享文件夹在Google云端硬盘上可能会变得棘手。

我甚至可以建议这样的事情并让脚本为你创建文件夹:

/*********************************************************
 * Function to determine the destination folder based on
 *   provided criteria.
 *
 * @param {Folder} root The root folder.
 * @param {String} folderYear The year of the report.
 * @param {String} folderMonth The month of the report.
 * @return {Folder} Destination Folder.
 *********************************************************/
function returnFolder(root, folderYear, folderMonth) {
  var dir      = DocsList.getFolderById(root);
  var folders  = DocsList.getFolderById(root).getFolders();
  var found    = false;
  var toReturn = "";

  for (var i = 0; i < folders.length; i++) {
    if (folders[i].getName() == folderYear) {
      dir      = folders[i];
      found    = true;
      break;
    }
  }

  if (!found) {
    dir = dir.createFolder(folderYear);
    folders = dir.getFolders();
  }
  else found = false;

  for (var i = 0; i < folders.length; i++)
    if (folders[i].getName() == folderMonth) {
      toReturn = folders[i].getId();
      found    = true;
      break;
    }

  if (!found) toReturn = dir.createFolder(folderMonth).getId();
  return DocsList.getFolderById(toReturn);
}

然后您的代码如下:

var SSID = '<redacted>'
var folder = returnFolder(OBJECT_PARENT_FOLDER, folderYear, folderMonth);
var backup = DocsList.getFileById(SSID).makeCopy(fname);
backup.addToFolder(folder); //This line will move the file to the appropriate folder
backup.removeFromFolder(DocsList.getRootFolder()); //This line is needed to remove the File from the Root

您需要根据需要匹配代码,但我希望这会有所帮助。