Display Form in Sidebar

限于喜欢 提交于 2019-12-23 04:28:59

问题


I am not sure if what I am trying to do is even possible.

I am trying to put a google form into the sidebar of a google doc.

Right now I have an apps script document plugin that opens the sidebar fine, and I am able to open the form using var form = FormApp.openById('form_id');

But I don't know how to get a html object or blob from a form element that I could use to embed the form in the sidebar.

I also can't use iframes as those are disallowed.


回答1:


While you can display a google form in a sidebar in a spreadsheet/document (see sample code below), there is one significant gotcha with it: the form does not work, i.e. you can't submit the form. Not sure exactly why, but my guess it is due to the sandbox restrictions and/or caja sanitization.

I suggest you code your own form using HTML and HTML Service (or UI Service) and show that in a sidebar. If you need you form to save the responses to a spreadsheet, you can also do that rather easily. See this for more on info and code samples of using forms in HTML Service and this for using forms in UI Service. Your server-side script can open a spreadsheet and write form values to it, if that's what you need to do.

Sample code for an Add-on to show Google Form in a Sidebar in Google Sheet:

NOTE: The form does not actually work - it does not submit when shown in a sidebar!

function showFormInSidebar() {
  var form = FormApp.openById('YOUR-FORM-ID-HERE');
  var formContent = UrlFetchApp.fetch(form.getPublishedUrl()).getContentText();
  var ui = HtmlService.createHtmlOutput(formContent).setTitle("Google Form in Sidebar Example");
  SpreadsheetApp.getUi().showSidebar(ui);
};

function onOpen(e) {
  // Add this add-on to Add-ons menu
  SpreadsheetApp.getUi().createAddonMenu()
      .addItem('Show form', 'showFormInSidebar')
      .addToUi();
};


来源:https://stackoverflow.com/questions/26327256/display-form-in-sidebar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!