Home > Software engineering >  Google Forms Apps Script (hover text not appearing nicely until refresh)
Google Forms Apps Script (hover text not appearing nicely until refresh)

Time:03-25

I have a working Google Apps script which is setting the helpText on an image item like this:

      form.addImageItem()
        .setImage(questionImages[i])
        .setTitle("Question "   position)
        .setHelpText(question.help)

This all works fine. The problem is that the help text can be quite long and when I insert it into the form it looks incorrectly sized ->

ugly hover

Until the page is refreshed, and then it is correctly sized and displays nicely ->

better display

Is there a way to force the page to "redraw"? I attempted to "refresh" the page with:

  var form = FormApp.getActiveForm();
  var id = form.getId();
  FormApp.openById(id);

but that did not work. I haven't seen any kind of "refresh" or similar function in the api, so, I'm out of ideas.

If anyone has solved this issue I'd love to hear it!

CodePudding user response:

I could replicate the same situation. In this case, it seems that when Google Fomr service (FormApp) is used, such a situation occurs.

But, fortunately, I could confirm that when Google Forms API is used, all texts can be shown when the long texts are put. So in this answer, I would like to propose the method for using Forms API.

Usage:

1. Linking Google Cloud Platform Project to Google Apps Script Project for New IDE.

In order to use Forms API, please link Google Cloud Platform Project to Google Apps Script Project for New IDE, and please enable Forms API at API console. Ref

2. Sample script.

function myFunction() {
  const formId = "###"; // Please set your Google Form ID.

  const url = `https://forms.googleapis.com/v1beta/forms/${formId}:batchUpdate`;
  const requestBody = {
    "requests": [
      {
        "createItem": {
          "item": {
            "imageItem": {
              "image": {
                "sourceUri": "https://stackoverflow.design/assets/img/logos/so/logo-stackoverflow.png",
                "altText": "sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample sample"
              }
            },
            "title": "sample title"
          },
          "location": {
            "index": 0
          }
        }
      }
    ]
  };
  const params = {
    method: "post",
    contentType: "application/json",
    headers: { authorization: "Bearer "   ScriptApp.getOAuthToken() },
    payload: JSON.stringify(requestBody),
    muteHttpExceptions: true
  };
  const res = UrlFetchApp.fetch(url, params);
  console.log(res.getContentText())
}
  • In this script, please include the scope of https://www.googleapis.com/auth/forms.body.

Note:

  • When you want to simply check this, you can also use "Try this method" of Forms API. In this case, please put the request body using my proposed script. By this, you can test it soon.

References:

  • Related