EDIT to try and make this easier to understand:
Here is what I did:
I created a Google Spreadsheet. I created a SCRIPT that saves it to a Google Drive Folder using a File Name based on the date of service and Customer Name.
This is the Script I currently have:
// This creates a custom Menu Function so that I can save the file.
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Choice Menu')
.addItem('Submit Form','saveAsSpreadsheet')
.addToUi(); }
// Saves Spreadsheet in Google Drive Folder
function saveAsSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B8xnkPYxGFbUMktOWm14TVA3Yjg");
DriveApp.getFileById(sheet.getId()).makeCopy(getFilename(), destFolder);
}
//This Function uses a cell "G4" (Which is Date and Customer Name) to create a file name. This file name will be then used for the above Script.
function getFilename() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getSheetByName('Manifest');
var cell = sheet.getRange('G4');
var filename = cell.getValue();
return filename;}
So My code works GREAT except for one Problem.
Here is my Problem:
When I Save the Spreadsheet a 2nd time using the above Script, it saves a new file. I want to save it as a new file unless the Filename is the same. IF the file name is the same, I want to delete the original file, and then save the new file.
What I tried: From what I understand reading the comments below, I need to run a code that "1. will need to run a query to see if any files exist with your chosen name, and then act accordingly. 2. If there are no files with that name, then go ahead and create one (using the function I listed above). If there is a file with the same name, 3. delete the original file and create a new one with the same name.
I tried for several days coming trying different options, and none of it worked, so I am looking to start over.
Appreciate any coding solutions or direction on where to go!
it replace (not create a copy)
? If there is a file with the duplicated filename in the folder, how do you want to do? 1. You want to delete the existing file and create new file with the same filename. In this case, the file ID is different from the original file. 2. You want to replace the file without changing the file ID. In the current stage, this cannot be achieved yet. – Tanaike