Responsive Ad Area

Share This Post

test

Apps Script – Create folder for each month, based on date value in a spreadsheet cell

I’m fairly new to Apps Script and managed to develop a script and assigned it to a button on my spreadsheet, when clicked it saves a copy of the active spreadsheet in a folder in G-Drive – This is working fine, however I’d like to accomplish a more complicated functionality.

A cell on the spreadsheet contains the current date, so when the button is clicked, I’d like the code to check for the date and create a new folder if its a new month and save a copy of the spreadsheet in the newly created folder. for ex: if the date is 1 Mar, 2019, the script should be able to create a folder with the name Mar and save a copy of the spreadsheet in that folder.

I’ve used the code below which I found here but it doesn’t do anything.

    function makeFolder() { 
   var formattedDate = Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd");
   var folderYearDate = Utilities.formatDate(new Date(), "GMT", "yyyy");
   var folderMonthDate = Utilities.formatDate(new Date(), "GMT", "MMMM");
   var name = formattedDate;
   var parent = DriveApp.getFolderById("xxxxxxxxxxx").getFoldersByName("SMS")
   var yearExist = parent.next();
   var yearFolders = yearExist.getFoldersByName(folderYearDate);
   if(yearFolders.hasNext()){
    var yearFolder = yearFolders.next();

   } else {
    var yearFolder = yearExist.createFolder(folderYearDate)
    return;

   }

   var monthFolders = yearFolder.getFoldersByName(folderMonthDate);
   if(monthFolders.hasNext()){
    var monthFolder = monthFolders.next()
    Logger.log('Yes, There is a month folder.')

   } else {
    var monthFolder = yearFolder.createFolder(folderMonthDate);
   }

   }

Any help is much appreciated


Apps Script – Create folder for each month, based on date value in a spreadsheet cell
Apps Script – Create folder for each month, based on date value in a spreadsheet cell
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar