问题
We currently have a nightly balancing process wherein we open an Excel template spreadsheet, enter the correct processing date, and then click a button, causing a VB script to fire off and create a new copy of the spreadsheet with the appropriate name (eg, "BAL 080114") in the appropriate folder and opens it, at which point the operator then closes the template, and continues work in the new copy.
The folder structure is:
Drive
--->Ops Room
------->Procedural Sheets
----------->Night Shift
--------------->Balancing
------------------->2014
----------------------->01-2014
...
----------------------->12-2014
We are trying to transition this to Google docs spreadsheets, and mostly it's working. But I cannot find a method to allow someone to open the template (stored in Balancing), run a "Start New Day" script, and have the script create the file in the proper sub-sub-folder. To wit, for 08/01/2014, the file should be stored in Balancing/2014/08-2014 as Bal 080114.
This is what I have thus far:
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
}
I borrowed the backup.* stuff from another StackOverFlow answer that had similar properties, but my version doesn't create the file.
Is what I'm trying to do even possible in Drive? Or will I just need to have the operators create a copy and then move it manually?
I apologize for any scripting ignorance - I've just started learning Google script this week, and I'm having trouble finding a common ground with my previous VB experience.
Thanks in advance for any help.
James
回答1:
I am not able to comment so I cannot add on to what is already there.
Are the folders that are "manually" being created being created with the correct permissions by the correct person? Shared Folders can get tricky on Google Drive.
I may even suggest something like this and have the script just create the folders for you:
/*********************************************************
* 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);
}
Then your code would be as follows:
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
You will need to match the code to your needs, but I hope this helps.
来源:https://stackoverflow.com/questions/25084463/i-need-to-save-a-copy-of-a-google-spreadsheet-to-a-specific-directory-using-scri