0

From inside one google sheet, I want to run a script (via script-attached button) that copies another,different, sheet into another, destination folder while naming the new file according to a cell value in the first sheet. Can anyone point me in the right direction? Here's what i have so far:

  function onOpen(e) {                   
  var jobTitle = SpreadsheetApp.getActiveSpreadsheet().getName();
  SpreadsheetApp.getActiveSpreadsheet().getRange("B3").setValue(jobTitle);
   }
  function CreateManFile8() {           
  var copyMan = DriveApp.getFileById("Source ID");                                  
  var toFolder = DriveApp.getFolderById("Destination ID");                                                   
  copyMan.makeCopy(toFolder);
   }

The first function is fine as it allows me to copy the first spreadsheet template, name it and then, onOpen, the file name goes into an appropriate cell of the input sheet from which it populates throughout the spreadsheet via standard sheets reference formulas and such. The second function just copies the "Man." template (from inside the first "Sales" template) and puts it into the appropriate folder without changing the name.

I need a way to name the Man. template according to the value in B3 (or any specified cell) from inside the Sales SpreadSheet template. I have the script attached to a button inside the Sales template but it just copies the second template and names it "Source ID";not the value of B3.

ISSUE BACKGROUND: (FOR THOSE INTERESTED)

Each of the projects I manage at work entail Sales processes contained in a "Sales Spreadsheet" Template composed of several inter-linked sheets and Management processes contained in a "Management Spreadsheet" Template composed of several other inter-linked (with standard formulas) sheets. Eventually, both of these Spreadsheet Templates are copied and used for every new project.

First, each Sales prospect engages sales processes that cause the Sales spreadsheet template to be copied, renamed and iteratively populated with relevant customer and sales data throughout the sales process. Then, at some point, contracts are signed which engage management processes and, consequently, the "Management Spreadsheet" Template is copied, named, and utilized for the project as well.

The idea is to have the "Man. Template" get copied from within the 'Sales Template" after the sales template has been manually named and populated with information collected during the sales process.

Most of the data collected, analyzed, and reported during the management phase of any project is different from the data needed to negotiate the sales processes but, the little bit that is redundant needs to be Programmatically transposed from the Sales Template to the Management Template for each project.

And So, I come to YOU...the stack overflow community for guidance. Any and all serious, service-oriented people offering help will be truly appreciated.

I know most of you are smarter than me. It does not help to be reminded. So, anyone Who actually wants to help a dude out just because they love coding and want to share it, I cant wait to hear from you.

Irk
  • 5
  • 5
  • Welcome to StackOverflow. Do you have a question? – Tedinoz Nov 09 '19 at 02:18
  • Take a range array and setvalues to any sheet in your spread sheet by that array, please attempt to create your script and inform us the problem. – user11982798 Nov 09 '19 at 02:30
  • man, you were quick! I will o – Irk Nov 09 '19 at 05:05
  • I will post what i have so far so you can take me seriously but no one, anywhere addresses the question. Im trying to articulate that I want to write a function that copies a different spreadsheet to another specified folder but names it according to a value in the first. The spreadsheets are different formats and I just want to know how to take values from inside the source and put them inside specific cells in another spreadsheet template. – Irk Nov 09 '19 at 05:16
  • Your question seems to be a duplicate of [How do I make copy of spreadsheet and save it to particular folder?](https://stackoverflow.com/q/31078758/1330560). – Tedinoz Nov 09 '19 at 07:02
  • Thanks Tedinoz. I will check it out. – Irk Nov 09 '19 at 18:09
  • I checked it out....not a duplicate. The reference above makes a copy of the file and sends it to appropriate folder but the copied file does not take the name of the file it is copied from. – Irk Nov 09 '19 at 19:31

2 Answers2

0

This just a sample, you must modify it for your purpose, this will copy a file from any folder to other folder with new name:

function CreateFile8() {
  //This assumes the folders are unique, so I don't use ID
  var folders=DriveApp.getFoldersByName('Sistem Folder'); //myfolder = 'my drive' > 'sistem folder'

  if (folders.hasNext())
  {
    var mySrcFolder = folders.next();
    var Destfolders = mySrcFolder.getFoldersByName('Test'); //myDestfolder = 'my drive' > 'sistem folder' > 'Test'
    var files=mySrcFolder.getFilesByName('Jojo'); //file jojo will be copied (only if not exist), and Jojo is as template
    if (files.hasNext() && Destfolders.hasNext())
    {
      var DestFolder = Destfolders.next();
      var myFile = files.next();
      var Destfiles=DestFolder.getFilesByName('Jobe'); //file jojo will be copied (only if not exist in dest), and Jojo is as template
      if (Destfiles.hasNext()==false)
      myFile.makeCopy('Jobe', DestFolder); //And jobe is extracted from your sheet, I don't adopt here to extract from your sheet
    }
  }  
}
user11982798
  • 1,878
  • 1
  • 6
  • 8
  • Thank you for taking a look. the code above does not address my issue though. – Irk Nov 09 '19 at 19:28
  • Maybe I need to restate my question because it does not seem like I am communicating the issue nor goal very well. – Irk Nov 09 '19 at 19:32
  • The idea is to have the "Man. Template" get copied from within the 'Sales Template" after the sales template has been manually named. So, we have two Spreadsheets that are templates, each composed of several tabs(sheets);lets call them 'Sales file-BLANK' and 'Man. File-BLANK'. Inside 'Sales file-BLANK' is a button used to copy 'Man. File- BLANK' and name it the same as 'Sale file-BLANK' s fIlename and then send it to another folder. – Irk Nov 09 '19 at 19:38
  • To clarify: 'Sales file-Blank' is a Spreadsheeet template having many individual sheets (Tabs). – Irk Nov 09 '19 at 19:41
  • and 'Man. file- Blank' is also a Spreadsheet template having many individual sheets (Tabs). – Irk Nov 09 '19 at 19:42
  • When new project comes our way, we copy the 'Sales file-Blank' and rename it the project name. Some time later the project goes into management and we use a button inside the copied, renamed 'Sales file-Blank' template to copy and name the 'Man. file-Blank' the same name as the renamed 'Sales file-Blank' template. As Man file is copied and named the same as the 'Sales file' we want to send it to the Man FOLDER (which holds all of the Man. files. How can we do this? – Irk Nov 09 '19 at 19:43
  • i think this is the best summary I've come up with so far. It seem to make more sense to me when i read it; So after we manually copy and name the Sales File-Blank template to create a new Sales file for the new project, we want to use a button inside this new Sales File to copy a different template (the Man File-Blank) and programmatically name it the same as the new Sales filename. Am i making sense with this? I appreciate all your help. – Irk Nov 09 '19 at 19:51
  • I can't answer your question perfectly, I just arrived to your second script, I don't have your another script, for first script, you just set a Cell B3 with name of active spread sheet. My script just take template, for example the man file blank – user11982798 Nov 09 '19 at 22:22
  • And in my script, Jojo as like as your man template, and Jobe is copied file of Jojo, and Jobe file name can can be take from your B3 Cell of your copied sales template that have renamed manually. Just it. – user11982798 Nov 09 '19 at 23:18
0

If I understood correctly, you want a function that makes a copy of the spreadsheet in an specified folder and with the value of the B3 cell as name.

You can try this modification of your function:

  function CreateManFile8() {           
     var copyMan = DriveApp.getFileById("spreadsheet Id").getId();    
     var toFolder = DriveApp.getFolderById("folder Id");       

     var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
     var sheetName = sheet.getRange('B3').getDisplayValue();

     DriveApp.getFileById(copyMan).makeCopy(sheetName, toFolder);  

}

You can check the documentation of makeCopy and getDisplayValue for more information.

Jescanellas
  • 2,555
  • 2
  • 9
  • 20
  • Hey Jes! Yes. From inside SS #1, I want to make a copy of a different template ,SS#2 and name the copied template (SS #2) the same as a value in SS #1. So we have 2 different templates SS #1 and SS #2. from inside #1, we want to copy and name SS#2 and then save the renamed #2 SS to a specified folder that holds all the other SS#2 copies. – Irk Nov 11 '19 at 17:20
  • 1
    Jes!!!! It works! Thank you !! Man, i was soooo close, but needed you and I thank you!!!!!! – Irk Nov 11 '19 at 17:51
  • Glad it does! I would appreciate if you could accept my solution, so others will see it works without reading the comments. Cheers! – Jescanellas Nov 12 '19 at 08:00
  • Im having difficulty adding to the code the ability to copy specified cells from SS1 to different cells in SS2. in addition to the Title .What am I missing? – Irk Nov 12 '19 at 18:07
  • You should use [getSheetByName()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getSheetByName(String)) to get the cell from Sheet1 and again to copy it to Sheet2. If you have more doubts don't hesitate to open a new post, as writing a solution in the comments might be confusing. – Jescanellas Nov 13 '19 at 14:36