用于检查是否已标记Google表单复选框的Google脚本

时间:2013-11-06 19:24:54

标签: google-apps-script google-form

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

问题
选择1
选择2
选择3

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

这是我的工作代码:

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


}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。

在Google表格电子表格中执行以下操作:

  1. 将选项分隔到电子表格中的不同列。
  2. 使用公式= ArrayFormula(IFERROR(SIGN(FIND(F $ 1; $ E2:$ E))))
    F $ 1 用于这种情况,因为选择在列F行1
    列E 原始列,其中询问了问题和回复从第2行开始,因此使用 $ E2:$ E
    它在匹配列的任何响应中放置“1”


  3. 在代码中我添加了更多变量。以下是整个代码:

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