在多个电子表格中共享相同的Google表单

时间:2017-05-09 09:34:14

标签: google-sheets google-spreadsheet-api google-form

我创建了一个反馈表单,希望从多个电子表格中打开并共享它。我在以下帖子Single Google Form for multiple Sheets中使用了修订版的代码。

表单按预期打开为IFRAME,但不允许我(或任何用户)填充字段并创建响应。我错过了什么?

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Data Architecture')
      .addItem('Create File Note', 'menuItem1')
      .addSeparator()
      .addSubMenu(ui.createMenu('Feedback & Actions')
          .addItem('Provide  Feedback', 'provideFeedback')
          .addItem('Create and Action', 'createAction'))        
      .addToUi();
}

function provideFeedback() {
  
  var documentName = SpreadsheetApp.getActive().getName();
  
  var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue();
  
  launchForm();
  
  
}

function createAction() {
  
  var documentName = SpreadsheetApp.getActive().getName()
  
  var documentID = SpreadsheetApp.getActive().getId()
  
  var dataentityName = SpreadsheetApp.getActiveRange().getCell(1, 1).getDisplayValue()
  
  launchForm();
  
}

function launchForm(){

  var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
  var form = FormApp.openById(formID);
  var formURL = form.getPublishedUrl();
   
  var response = UrlFetchApp.fetch(formURL);
  var formHtml = response.getContentText();
  
  var htmlApp = HtmlService
  .createHtmlOutput(formHtml)
  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
  .setTitle('Feedback and action form')
  .setWidth(800)
  .setHeight(800);
  
  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

1 个答案:

答案 0 :(得分:0)

您可以将带有src的iframe标记用于您的网址,而不是访问网页并生成HTML内容并再次提供。

function launchForm(){

  var formID='1pmH3AWiMUczat5uIaZ5zaT--cmDjq9v3W9pePPjwGF0';
  var form = FormApp.openById(formID);
  var formURL = form.getPublishedUrl()+ "?embedded=true" 

  var htmlApp = HtmlService
  .createHtmlOutput('<iframe src="'+formURL+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
  .setTitle('Feedback and action form')
  .setWidth(400)
  .setHeight(400);

  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

要在脚本中使用预填表格:
1)按照here步骤获取预填表格 2)对于这样的表格:

enter image description here    预填充的URL将如下所示:

var formURL = "https://docs.google.com/forms/d/e/{Publish ID}/viewform?
usp=pp_url&
entry.1739076204=Jack+Brown&
entry.1869878870=2017-05-12&
entry.799903038=Incorrect&
entry.1996064114=I+am+the+best&
entry.607023772=Select+Me"

我已将单个选项分开,以便更容易与表单中的条目进行比较。现在要修改条目,您只需要修改&#39; =&#39;之后的值。每个条目的标志。所以要更改你要做的名字:

var name = "Chuck Norris"
entry.1739076204='+name+'

同样,您可以通过修改URL来修改每个输入。以下代码修改了我托管的特定表单的条目。您必须根据需要修改

 function launchForm(){

  var publishedURl = "https://docs.google.com/forms/d/e/1FAIpQLSdsHM53jTWJ0Eqn8VSxE5bWHjnD9KXsVBrqLsBwtyJIIsjpnA/viewform"
  var name = "Chuck Not Norris"
  var dt = Utilities.formatDate(new Date(), "GMT-05:00", "yyyy-MM-dd")
  var options1 = ["Correct","Incorrect"]
  var options2 = "Chuck Norris is google Apps Scripts"
  var options3 = ["Select Me","Don't select me"]
  var preEditUrl = publishedURl + '?usp=pp_url&entry.1739076204='+name
                   +'&entry.1869878870='+dt
                   +'&entry.799903038='+options1[0]
                   +'&entry.1996064114='+options2
                   +'&entry.607023772='+options3[0]

  var htmlApp = HtmlService
  .createHtmlOutput('<iframe src="'+preEditUrl+'" width="500" height="400" frameborder="0" marginheight="0" marginwidth="0"> </iframe>')
  .setTitle('Feedback and action form')
  .setWidth(400)
  .setHeight(400);

  SpreadsheetApp.getActiveSpreadsheet().show(htmlApp);
}

希望这会让你开始朝着正确的方向前进。