0

我有一个从 Google 表单电子表格合并到 PDF 的 Google 脚本。是否可以在 Google Apps 脚本中检查复选框是否被标记,复制表单。

问题
选择 1
选择 2
选择 3

基本上,它在这里工作:有一个谷歌文档模板,其中包含用户在谷歌表单中填写的任何内容的密钥。该脚本引用电子表格和模板。

这是我的工作代码:

//Get template and name it
var docTemplate = "id";
var docName = "name";


//Reference Spreadsheet
function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}


//Form submittal
function onFormSubmit() {
//spreadsheet values
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  var email = sheet.getRange(row, getColIndexByName("E-mail:")).getValue();
  var title = sheet.getRange(row, getColIndexByName("Title of Proposed Request (problem      / opportunity):")).getValue();
  var school = sheet.getRange(row, getColIndexByName("School")).getValue();
  var office = sheet.getRange(row, getColIndexByName("Office")).getValue();
  var facproblems =sheet.getRange(row, getColIndexByName("Which of the following best  describes the facilities-related problem (or opportunity) your School/Office has identified?")).getValue();
  var uses = sheet.getRange(row, getColIndexByName("Please list what other uses /     functions need to be adjacent to your program/activity.")).getValue();
  var describeProblem = sheet.getRange(row, getColIndexByName("a) Describe the identified problem or opportunity.")).getValue();
  var impacts = sheet.getRange(row, getColIndexByName("b) Part I: Provide justification and evidence in support of the identified problem/opportunity. Impacts - select one or more from the below list.")).getValue();
  var impactsExplain = sheet.getRange(row, getColIndexByName("b) Part II: Provide justification and evidence in support of the identified problem/opportunity. Explain how the proposed project will achieve the impacts you indicated above.")).getValue();
  var advance = sheet.getRange(row, getColIndexByName("c) Explain how the requested project will advance the College’s Strategic Plan and/or the requesting school or office’s academic plan and / or business plan.")).getValue();
  var estimate = sheet.getRange(row, getColIndexByName("d) Part I: What is your estimate of the potential cost (in rough order of magnitude) of addressing the problem or opportunity described above?  ")).getValue();
  var estimateExplain = sheet.getRange(row, getColIndexByName("d) Part II: Please explain how your developed this estimate.")).getValue();
  var requestor = sheet.getRange(row, getColIndexByName("Name of Requestor/Proposer")).getValue();
  var date = sheet.getRange(row, getColIndexByName("Date")).getValue();
  var title = sheet.getRange(row, getColIndexByName("Title of Proposed Request (problem / opportunity):")).getValue();
  var phone = sheet.getRange(row, getColIndexByName("Phone No.")).getValue();
  var sponsor = sheet.getRange(row, getColIndexByName("Name of Project Sponsor")).getValue();


  //copy and combine doc name and name form
  var copyId = DocsList.getFileById(docTemplate)
  .makeCopy(docName+' for '+requestor)
  .getId();

//open temp doc and copy body
 var copyDoc = DocumentApp.openById(copyId);
 var copyBody = copyDoc.getActiveSection();

  //replace placeholderkeys from template
  copyBody.replaceText('keyProposedTitle', title);
  copyBody.replaceText('keySchool', school);
  copyBody.replaceText('keyOffice', office);
  copyBody.replaceText('keyFacProblem', facproblems);
  copyBody.replaceText('keyUses', uses);
  copyBody.replaceText('keyDescribeProblem', describeProblem);
  copyBody.replaceText('keyImpacts', impacts);
  copyBody.replaceText('keyImpactsExplain', impactsExplain);
  copyBody.replaceText('keyAdvance', advance);
  copyBody.replaceText('keyEstimate', estimate);
  copyBody.replaceText('keyEstimateExplain', estimateExplain);
  copyBody.replaceText('keyRequestor', requestor);
  copyBody.replaceText('keyDate', date);
  copyBody.replaceText('keyPhone', phone);
  copyBody.replaceText('keyEmail', email);
  copyBody.replaceText('keySponsor', sponsor);

 //save and close doc
  copyDoc.saveAndClose();

//create PDF of temp doc
  var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

//email recipient
  var subject = "Manhattan College Space Planning Form Submittal";
  var body = "Thanks for submitting your request. \n\nPlease find an attachment with the details given."
  MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});

//trash temp doc
  DocsList.getFileById(copyId).setTrashed(true); 


}

谢谢你的帮助。

4

1 回答 1

1

我想到了。

在 Google 表单电子表格上执行了以下操作:

  1. 将选项分成电子表格中的不同列。
  2. 使用公式 = ArrayFormula(IFERROR(SIGN(FIND(F$1;$E2:$E)))) 在这种情况下使用
    F$1 ,因为选择在F 列第 1 行
    E列是问题所在的原始列询问并且响应从第 2 行开始,因此使用$E2:$E。它在与该列匹配的任何响应中
    放置一个“1” 。


在代码中,我添加了更多变量。下面是整个代码:

//Get template and name it
var docTemplate = "1n9ytnC2bNrkPZ62pNiyi3Xy6GcDRvGPLZRMfJxsLY1g";
var docName = "ManhattanCollegeSpacePlanning";


//Reference Spreadsheet
function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}


//Form submittal
function onFormSubmit() {
//spreadsheet values
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  var email = sheet.getRange(row, getColIndexByName("E-mail:")).getValue();
  var title = sheet.getRange(row, getColIndexByName("Title of Proposed Request (problem / opportunity):")).getValue();
  var school = sheet.getRange(row, getColIndexByName("School")).getValue();
  var office = sheet.getRange(row, getColIndexByName("Office")).getValue();
  var addSpaceExisting = sheet.getRange(row, getColIndexByName("Additional space is needed for existing projects/programs/activities.")).getValue();
  var addSpaceNew = sheet.getRange(row, getColIndexByName("Additional space is needed for new projects/programs/activities.")).getValue();
  var currentSpace = sheet.getRange(row, getColIndexByName("Current space is substandard or inappropriate")).getValue();
  var relocation = sheet.getRange(row, getColIndexByName("Relocation required for better adjacencies (list what other uses / functions need to be adjacent to your program/activity)")).getValue();
  var displacement = sheet.getRange(row, getColIndexByName("Displacement from existing space – relocation required.")).getValue();
  var other = sheet.getRange(row, getColIndexByName("Other (not described in other categories above).")).getValue();
  var uses = sheet.getRange(row, getColIndexByName("Please list what other uses / functions need to be adjacent to your program/activity.")).getValue();
  var describeProblem = sheet.getRange(row, getColIndexByName("a) Describe the identified problem or opportunity.")).getValue();
  var impacts = sheet.getRange(row, getColIndexByName("b) Part I: Provide justification and evidence in support of the identified problem/opportunity. Impacts - select one or more from the below list.")).getValue();
  var impactsExplain = sheet.getRange(row, getColIndexByName("b) Part II: Provide justification and evidence in support of the identified problem/opportunity. Explain how the proposed project will achieve the impacts you indicated above.")).getValue();
  var advance = sheet.getRange(row, getColIndexByName("c) Explain how the requested project will advance the College’s Strategic Plan and/or the requesting school or office’s academic plan and / or business plan.")).getValue();
  var estimate = sheet.getRange(row, getColIndexByName("d) Part I: What is your estimate of the potential cost (in rough order of magnitude) of addressing the problem or opportunity described above?  ")).getValue();
  var estimateExplain = sheet.getRange(row, getColIndexByName("d) Part II: Please explain how your developed this estimate.")).getValue();
  var requestor = sheet.getRange(row, getColIndexByName("Name of Requestor/Proposer")).getValue();
  var date = sheet.getRange(row, getColIndexByName("Date")).getValue();
  var title = sheet.getRange(row, getColIndexByName("Title of Proposed Request (problem / opportunity):")).getValue();
  var phone = sheet.getRange(row, getColIndexByName("Phone No.")).getValue();
  var sponsor = sheet.getRange(row, getColIndexByName("Name of Project Sponsor")).getValue();


  //copy and combine doc name and name form
  var copyId = DocsList.getFileById(docTemplate)
  .makeCopy(docName+' for '+requestor)
   .getId();

//open temp doc and copy body
 var copyDoc = DocumentApp.openById(copyId);
 var copyBody = copyDoc.getActiveSection();

  //replace placeholderkeys from template
  copyBody.replaceText('keyProposedTitle', title);
  copyBody.replaceText('keySchool', school);
  copyBody.replaceText('keyOffice', office);
  copyBody.replaceText('keyAddSpaceExisting', addSpaceExisting);
  copyBody.replaceText('keyAddSpaceNew', addSpaceNew);
  copyBody.replaceText('keyCurrentSpace', currentSpace);
  copyBody.replaceText('keyRelocation', relocation);
  copyBody.replaceText('keyDisplacement', displacement);
  copyBody.replaceText('keyOther', other);
  copyBody.replaceText('keyUses', uses);
  copyBody.replaceText('keyDescribeProblem', describeProblem);
  copyBody.replaceText('keyImpacts', impacts);
  copyBody.replaceText('keyImpactsExplain', impactsExplain);
  copyBody.replaceText('keyAdvance', advance);
  copyBody.replaceText('keyEstimate', estimate);
  copyBody.replaceText('keyEstimateExplain', estimateExplain);
  copyBody.replaceText('keyRequestor', requestor);
  copyBody.replaceText('keyDate', date);
  copyBody.replaceText('keyPhone', phone);
  copyBody.replaceText('keyEmail', email);
  copyBody.replaceText('keySponsor', sponsor);

 //save and close doc
 copyDoc.saveAndClose();

//create PDF of temp doc
  var pdf = DocsList.getFileById(copyId).getAs("application/pdf");

//email recipient
  var subject = "Manhattan College Space Planning Form Submittal";
  var body = "Thanks for submitting your request. \n\nPlease find an attachment with the details given."
  MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});

//trash temp doc
 DocsList.getFileById(copyId).setTrashed(true); 


}
于 2013-11-07T16:26:42.547 回答