【问题标题】:Google Sheets Script - define constant based on text in cellGoogle Sheets Script - 根据单元格中的文本定义常量
【发布时间】:2021-01-15 18:11:00
【问题描述】:

我希望有人可以帮助我。 所以基本上这个函数有一个基于谷歌表单应用程序的触发器。 在表单中,我得到了一个包含 5 个名称的下拉菜单。 这些名称总是放在 Google 表格的第 1 行。 现在脚本使用工作表中的数据创建一个文档并将其放入特定文件夹(const destinationFolder) - 我现在的目标是根据从下拉列表中选择的名称更改destinationFolder - 所以我得到了5种不同的文本变体在应该导致 5 个不同目标文件夹的表格单元格(5 个名称)中。我如何定义它?非常感谢您帮助我!

这是现在的代码(我从 Jeff Everhart 的 YT 视频中得到它):

 //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)
  
  })
 
}

【问题讨论】:

  • 欢迎来到 StackOverflow!您自己是否已经研究过 Google App Script 基础知识?你在哪里卡住了?抱歉,如果我误解了您的问题,但请记住,当您已经尝试过一段特定的代码时,这个网站更多的是针对具体问题,所以以防万一您更卡在“我如何编程”完全”阶段(如果不是这种情况,我深表歉意),那么首先投入更多时间以获得更好的回复以及稍后更详细的查询可能会有所帮助。

标签: google-apps-script google-sheets


【解决方案1】:

您可以简单地创建一个key-value pair in javascript,它将为每个用户定义您的文件夹 ID。只需要以用户名作为key,就可以得到对应的文件夹id。

示例实现:

// 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]);

样本表:

输出:

你的代码应该是这样的:

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)
   
   })
  
}

【讨论】:

  • 我还有一个问题,这就是它在工作表中的完成方式:imgur.com/alwrzYd - 所以名称是垂直的,所以我不能真的去 A1,B1 因为每次有新的时候它都会改变
  • 根据我的理解,您已经在此代码中获得了用户名body.replaceText('{{NAME}}', row[2]);,您只需使用row[2] 访问工作表中的名称并将其实现为userFolder[row[2]] 即可获得定义了相应的目标文件夹。
  • 我修改了你的代码 sn-p,请查看更新后的答案。
  • 对不起,我觉得自己很愚蠢,因为现在应该很容易做到这一点,就像你告诉我的那样,但我不太明白。但我明白你是否回答完这个问题^^
  • 没关系,如果你已经成功了,请告诉我。请同时确认用户的姓名是否真的在第 [2] 行,因为我只是假设它基于 body.replaceText('{{NAME}}', row[2]);
猜你喜欢
  • 2020-09-20
  • 1970-01-01
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多