get form URL from a spreadsheet bound Form

人走茶凉 提交于 2019-11-29 12:36:56

Because you are working in the Spreadsheet, you need to get the associated form from the Spreadsheet object (e.g. SpreadsheetApp.getActiveSpreadsheet.getFormUrl() ).

You will also need to send the mail message with the htmlBody optional parameter.

Here is a code sniplet:

function sendNotice(recipient){
  try{

    // either hardcode the folder id below
    var formStorageFolderId = '';

    // or programmatically get the folder from the spreadsheet parent
    var ss = SpreadsheetApp.getActiveSpreadsheet();

    var ssFolder = DriveApp.getFileById(ss.getId()).getParents();
    if(ssFolder.hasNext()){
      // assume there is only one parent folder
      formStorageFolderId = ssFolder.next().getId();
    }

    var formFolder = DriveApp.getFolderById(formStorageFolderId);

    var files = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
    var formId = '';
    while(files.hasNext()){
      // search for the form (is it the same name as the spreadsheet?)
      var file = files.next();
      var fileName = file.getName();
      var sheetName = ss.getName();
      if(fileName == sheetName){
        // matched names

        formId = file.getId();
        break;
      }
    }
    if(formId){
      var actualForm = FormApp.openById(formId);
      var formName = actualForm.getTitle();
      var formURL = actualForm.getPublishedUrl();

      var subject = "Please fill out form";
      // html mail message (needed for the link
      var mailBody = '<div><p>Please fill out the attached form<p>';
      mailBody += '<p><a href="' + formURL +'">' + formName + '</a>';

      MailApp.sendEmail(recipient, subject, '',{htmlBody:mailBody});
    }

  }catch(err){
    Logger.log(err.lineNumber + ' - ' + err);
  }
}

If there is one form linked to spreadsheet

Use

var ss = SpreadsheetApp.getActiveSpreadsheet(); // or openById, etc
var formUrl = ss.getFormUrl();

to get its Url. If needed, FormApp.openByUrl(formUrl); returns a pointer to the form, which allows any other methods.

Multiple forms linked to spreadsheet

There is no built-in method to return the list of forms linked to a given spreadsheet; this is an open issue in Apps Script issues tracker. A workaround is to search all forms (as Cyrus Loree did), get the destination of each, and return the list of those where the destination is the spreadsheet of interest. This is how:

function linkedForms() {
  var ssId = SpreadsheetApp.getActiveSpreadsheet().getId();
  var formList = [];
  var files = DriveApp.getFilesByType(MimeType.GOOGLE_FORMS);
  while (files.hasNext()) {
    var form = FormApp.openByUrl(files.next().getUrl());
    try {
      if (form.getDestinationId() == ssId) {
        formList.push(form.getPublishedUrl());
      }
    }
    catch(e) {
    }
  }
  return formList;
}

Remarks:

  • I put form.getDestinationId() in a try block because this method throws an error what the form's destination spreadsheet happens to be deleted (instead of just returning null)
  • To get the list of form Ids instead of Urls, use form.getId() in the function.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!