Advertisement
mentoly

Make Backup Copy With Menu

Jan 18th, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Program: Archive GSheet with Date Stamp
  2. // Programmer: Michael Fryar
  3. // Date: 19 September 2017
  4. // Google Apps Script to copy Google Sheet to subfolder with date stamp in name
  5.  
  6. // Add Custom Menu
  7. function onOpen() {
  8.   var ui = SpreadsheetApp.getUi();
  9.  
  10.   ui.createMenu('Archive')
  11.       .addItem('Archive Copy with Date Stamp', 'archiveCopy')
  12.       .addItem('Open Archive Folder', 'openArchive')
  13.       .addToUi();
  14. }
  15.  
  16. // Define function to copy sheet to subfolder with date stamp in name
  17. // Building on https://gist.github.com/abhijeetchopra/99a11fb6016a70287112
  18. function archiveCopy() {
  19.  
  20.   // Replace "spreadsheetId" with the ID of the Google Sheet you wish to copy
  21.   var file = DriveApp.getFileById("1yQGyrwDWC0Xp3fUDl0heD5IEz5IumpjilrZnzlLN2jc")
  22.  
  23.   // Replace "folderId" with the ID of the folder where you want the copy saved
  24.   var destination = DriveApp.getFolderById("1-rzU0g-oRNaVHTClM1LvQDVCnsdSftfw");
  25.  
  26.   // Get timezone for datestamp
  27.   var timeZone = Session.getScriptTimeZone();
  28.  
  29.   // Generate datestamp and store in variable formattedDate as year-month-date
  30.   var formattedDate = Utilities.formatDate(new Date(), timeZone , "yyyy-MM-dd HH-mm");
  31.  
  32.   // Replace "file_name" with the name you want to give the copy
  33.   var name = formattedDate + " - API Trigger Backup";
  34.  
  35.   // Archive copy of "file" with "name" at the "destination"
  36.   file.makeCopy(name, destination);
  37. }
  38.  
  39. // Define function to open archive folder in new tab
  40. // Building on https://www.youtube.com/watch?v=2y7Y5hwmPc4
  41. function openArchive() {
  42.  
  43.   // Replace "folderId" with the ID of the folder where you want copies saved
  44.   var url = "https://drive.google.com/drive/folders/1-rzU0g-oRNaVHTClM1LvQDVCnsdSftfw"
  45.  
  46.   // HTML to open folder url in new tab and then close dialogue window in sheet
  47.   var html = "<script>window.open('" + url + "');google.script.host.close();</script>";
  48.  
  49.   // Push HTML into user interface
  50.   var userInterface = HtmlService.createHtmlOutput(html);
  51.   SpreadsheetApp.getUi().showModalDialog(userInterface, 'Opening Archive Folder');
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement