-1

i hope someone can help me. So bascically the function has a trigger based on a google forms application. In the Forms i got a Dropdown Menu with 5 Names. These Names get always get put in Row 1 of the Google Sheet. Now the Script creates a Doc with the Data from the Sheet and puts it in a specific Folder (const destinantionFolder) - My Goal now is to change the destinationFolder based on the Name that is selcted from the Dropdown - so i got 5 different text variations in the Sheets Cell (The 5 Names) that should lead to 5 different destinationFolders. How do i define that? Huge thanks for helping me!

this is the code rightnow (i got it from a YT Video from Jeff Everhart):

 //This value should be the id of your document template that we created in the last step
 const googleDocTemplate = DriveApp.getFileById('112VRkj6msylp-vwaSmHqLNQDosCBsP0HaMxxxx');
 
 //This value should be the id of the folder where you want your completed documents stored

 const destinationFolder = DriveApp.getFolderById('1wI4QEZ6iC3Ur9CgTZtg4DmMfkxxxxx')
 //Here we store the sheet as a variable
 const sheet = SpreadsheetApp
   .getActiveSpreadsheet()
   .getSheetByName('Data')
 
 //Now we get all of the values as a 2D array
 const rows = sheet.getDataRange().getValues();
 
 //Start processing each spreadsheet row
 rows.forEach(function(row, index){
   //Here we check if this row is the headers, if so we skip it
   if (index === 0) return;
   //Here we check if a document has already been generated by looking at 'Document Link', if so we skip 
   it
   if (row[14]) return;
   //Using the row data in a template literal, we make a copy of our template document in our 
   destinationFolder
   const copy = googleDocTemplate.makeCopy(`${row[2]} - Infos` , destinationFolder)
   //Once we have the copy, we then open it using the DocumentApp
   const doc = DocumentApp.openById(copy.getId())
   //All of the content lives in the body, so we get that for editing
   const body = doc.getBody();
   
   //In these lines, we replace our replacement tokens with values from our spreadsheet row
   body.replaceText('{{Zeitstempel}}', row[0]);
   body.replaceText('{{NAME}}', row[2]);
   body.replaceText('{{Intro}}', row[5]);
   body.replaceText('{{Ziel}}', row[6]);
   body.replaceText('{{Abgehalten}}', row[7]);
   body.replaceText('{{Kooperation}}', row[8]);
   body.replaceText('{{FinanzZiel}}', row[9]);
   body.replaceText('{{Invest}}', row[10]);
   body.replaceText('{{Invest20}}', row[11]);
   body.replaceText('{{Notizen}}', row[3]);
   body.replaceText('{{Startzeit}}', row[13]);
 

   //We make our changes permanent by saving and closing the document
   doc.saveAndClose();
   //Store the url of our new document in a variable
   const url = doc.getUrl();
   //Write that value back to the 'Document Link' column in the spreadsheet. 
   sheet.getRange(index + 1, 15).setValue(url)
   
   })
 
   }```

And thats the new new new Code:

   function createNewGoogleDocs() {
 //This value should be the id of your document template that we created in the last step
 const googleDocTemplate = DriveApp.getFileById('TemplateID');
 
 //This value should be the id of the folder where you want your completed documents stored
// Define folder id per user
 const userFolder = {
   Alina: "1ImNRXlyaFPGEatDMgi4cQ2JhPxxxxx",
   Cem: "1xHhxupTXD8KRYkSk2Lll31pDcQxxxxx",
   Constantin: "1wI4QEZ6iC3Ur9CgTZtg4DmMfkxxxxx",
   Marie: "1mvZbp-CQP-oWsVeVv7Cc2htXExxxxx",
   Johanna: "FolderIDxxxxx2",
 };

 //Here we store the sheet as a variable
 const sheet = SpreadsheetApp
   .getActiveSpreadsheet()
   .getSheetByName('Data')
 
 //Now we get all of the values as a 2D array
 const rows = sheet.getDataRange().getValues();
 //Logger.log(rows);
//Start processing each spreadsheet row
rows.forEach(function(row, index){
  //Here we check if this row is the headers, if so we skip it
  if (index === 0) return;

  Logger.log(row[14]);
  //Here we check if a document has already been generated by looking at 'Document Link', if so we skip 
 
  if (row[14]) return;

  //Using the row data in a template literal, we make a copy of our template document in our 
  Logger.log(row[1]);
  Logger.log(userFolder[row[1]]);
  const destinationFolder = DriveApp.getFolderById(userFolder[row[1]]);
  

  const copy = googleDocTemplate.makeCopy(`${row[2]} - Infos` , destinationFolder)
  //Once we have the copy, we then open it using the DocumentApp
  const doc = DocumentApp.openById(copy.getId())
  //All of the content lives in the body, so we get that for editing
  const body = doc.getBody();
  
  //In these lines, we replace our replacement tokens with values from our spreadsheet row
  body.replaceText('{{Zeitstempel}}', row[0]);
  body.replaceText('{{NAME}}', row[2]);
  body.replaceText('{{Intro}}', row[5]);
  body.replaceText('{{Ziel}}', row[6]);
  body.replaceText('{{Abgehalten}}', row[7]);
  body.replaceText('{{Kooperation}}', row[8]);
  body.replaceText('{{FinanzZiel}}', row[9]);
  body.replaceText('{{Invest}}', row[10]);
  body.replaceText('{{Invest20}}', row[11]);
  body.replaceText('{{Notizen}}', row[3]);
  body.replaceText('{{Startzeit}}', row[13]);


  //We make our changes permanent by saving and closing the document
  doc.saveAndClose();
  //Store the url of our new document in a variable
  const url = doc.getUrl();
  //Write that value back to the 'Document Link' column in the spreadsheet. 
  sheet.getRange(index + 1, 15).setValue(url)
  
  })
 
}
  • 1
    Welcome to StackOverflow! Have you already looked into Google App Script basics yourself? Where did you get stuck? Sorry if I'm misinterpreting your question, but keep in mind that this site is more for like concrete issues when you've already tried a specific piece of code, so just in case you're more stuck on the "how do I program at all" stage (I deeply apologize if that is not the case) then it might be helpful to invest some more time first to get better responses here with more detailed queries later. – E. T. Jan 15 '21 at 18:52

1 Answers1

2

You can simply create a key-value pair in javascript which will define your folder id per user. You just need to use the name of the user as a key to get its corresponding folder id.

Example Implementation:

// Define folder id per user
  const userFolder = {
    User1: "folderId1",
    User2: "folderId2"
  }

  //Get the user name in row 1 of your sheet
  var user1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("A1").getValue();
  var user2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("B1").getValue();
  
  Logger.log(user1);
  Logger.log(userFolder[user1]);
  Logger.log(user2);
  Logger.log(userFolder[user2]);

  //Get destination folder based on user name
  const destinationFolder = DriveApp.getFolderById(userFolder[user1]);

Sample Sheet:

enter image description here

Output:

enter image description here

Your code should be like this:

function createNewGoogleDocs() {
  //This value should be the id of your document template that we created in the last step
  const googleDocTemplate = DriveApp.getFileById('sample id');
  
  //This value should be the id of the folder where you want your completed documents stored
// Define folder id per user
  const userFolder = {
    Alina: "sample id",
    Cem: "sample id",
    Constantin: "sample id",
    Marie: "sample id",
    Johanna: "sample id",
  };

  //Here we store the sheet as a variable
  const sheet = SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheetByName('Data')
  
  //Now we get all of the values as a 2D array
  const rows = sheet.getDataRange().getValues();
  //Logger.log(rows);
//Start processing each spreadsheet row
 rows.forEach(function(row, index){
   //Here we check if this row is the headers, if so we skip it
   if (index === 0) return;

   Logger.log(row[14]);
   //Here we check if a document has already been generated by looking at 'Document Link', if so we skip 
  
   if (row[14]) return;

   //Using the row data in a template literal, we make a copy of our template document in our 
   Logger.log(row[1]);
   Logger.log(userFolder[row[1]]);
   const destinationFolder = DriveApp.getFolderById(userFolder[row[1]]);
   

   const copy = googleDocTemplate.makeCopy(`${row[2]} - Infos` , destinationFolder)
   //Once we have the copy, we then open it using the DocumentApp
   const doc = DocumentApp.openById(copy.getId())
   //All of the content lives in the body, so we get that for editing
   const body = doc.getBody();
   
   //In these lines, we replace our replacement tokens with values from our spreadsheet row
   body.replaceText('{{Zeitstempel}}', row[0]);
   body.replaceText('{{NAME}}', row[2]);
   body.replaceText('{{Intro}}', row[5]);
   body.replaceText('{{Ziel}}', row[6]);
   body.replaceText('{{Abgehalten}}', row[7]);
   body.replaceText('{{Kooperation}}', row[8]);
   body.replaceText('{{FinanzZiel}}', row[9]);
   body.replaceText('{{Invest}}', row[10]);
   body.replaceText('{{Invest20}}', row[11]);
   body.replaceText('{{Notizen}}', row[3]);
   body.replaceText('{{Startzeit}}', row[13]);
 

   //We make our changes permanent by saving and closing the document
   doc.saveAndClose();
   //Store the url of our new document in a variable
   const url = doc.getUrl();
   //Write that value back to the 'Document Link' column in the spreadsheet. 
   sheet.getRange(index + 1, 15).setValue(url)
   
   })
  
}
Ron M
  • 5,791
  • 1
  • 4
  • 16
  • I still got a Problem, this is how its done in the sheet: https://imgur.com/alwrzYd - So the names are Vertical, so i cant really go A1, B1 because it changes everytime there is a new one – Ché Algarez Jan 15 '21 at 18:57
  • Based on my understanding you already got the user name in this code `body.replaceText('{{NAME}}', row[2]);`, you just need to use `row[2]` to access the names in your sheet and implement it as `userFolder[row[2]]` to get the corresponding destination folder defined. – Ron M Jan 15 '21 at 19:00
  • I modified your code snippet, please see updated answer. – Ron M Jan 15 '21 at 19:05
  • im sorry i feel quite stupid because it should be very easy to do it now as you told me how, but i dont quite get it. But i understand if u are done with answering this question ^^ – Ché Algarez Jan 15 '21 at 19:08
  • 1
    Its ok, please let me know if you already done it successfully. please confirm as well if the name of the user was really in row[2] because i just assume it based on the `body.replaceText('{{NAME}}', row[2]);` – Ron M Jan 15 '21 at 19:10
  • ITs actually row 1 - i changed it, i posted the new code via editing the original post, if u scroll down. I dont know why it doesnt work now :/ – Ché Algarez Jan 15 '21 at 19:30
  • can you share a copy of your sheets? – Ron M Jan 15 '21 at 19:33
  • could you find the problem? :) – Ché Algarez Jan 15 '21 at 20:21
  • found some issues, you try to access row1 before the forEach(). I can't tell if that will work since i don't have access in the drive folders. But please try (i changed the folder id for privacy, just replace it again) – Ron M Jan 15 '21 at 20:25
  • i dont see that anything changed :/ you can see if it works by looking at the last row Document link - if it works the URL to the File will be inserted there – Ché Algarez Jan 15 '21 at 20:28
  • i removed this `Logger.log(Alina);` Alina is not defined, if you want to try to access the `userFolder[]` it should be in array type `userFolder["Alina"]`. Another thing `const destinationFolder = DriveApp.getFolderById(userFolder[row1]);` you tried to use row1 in line 35 which is not defined. Your procedure inside `rows.forEach()` seems correct – Ron M Jan 15 '21 at 20:32
  • did you include comma ',' after each key-value? originally it has missing comma – Ron M Jan 15 '21 at 20:33
  • i actually didnt, i added it now but it still doesnt work :/ – Ché Algarez Jan 15 '21 at 20:37
  • i added the current code to the original post again - i replaced the "old-new code" .. ^^ – Ché Algarez Jan 15 '21 at 20:38
  • the current code which is commented out should work. I don't see any issue anymore, what is the error message that you see when you run createNewGoogleDocs()? – Ron M Jan 15 '21 at 20:41
  • i sadly cant use the stack overflow chat since i have not got enough reputation yet :/ – Ché Algarez Jan 15 '21 at 20:41
  • there is no error message anymore, but it just doesnt work :/ No Doc gets created and no Doc Link gets Put into the "Document Link" Row, the folders where they should be created are just empty – Ché Algarez Jan 15 '21 at 20:44
  • by the way, please remove private information in your post like the folder ids' – Ron M Jan 15 '21 at 20:45
  • can you confirm if the destination folders are inside your Google Drive? or is it in a shared drive? please add logger.log() as well to check if url was generated after saving and closing the document – Ron M Jan 15 '21 at 20:46
  • it is in my google drive, i also added logger.log() now – Ché Algarez Jan 15 '21 at 20:51
  • please also check the folder ids' if it is correct, and check if there is a valid value for copy.getId(). also if logger.log(destinationFolder) it should display a Folder type – Ron M Jan 15 '21 at 20:53
  • im sorry for taking so much of your time, im probably the most uneducated and most unexpirienced person here :/ – Ché Algarez Jan 15 '21 at 20:54
  • it's ok, it is quite difficult debugging through chat – Ron M Jan 15 '21 at 20:55
  • i actually think it works!! there was an ' missing around the names, as you told me like 6 messages ago.. – Ché Algarez Jan 15 '21 at 20:59