79610207

Date: 2025-05-07 09:38:00
Score: 0.5
Natty:
Report link

Create Custom Forms and Insert the response to Docs Place Holders

What you need is to create a custom Menu then use a Modal Dialog to create a pop up window then use the Google Docs built in Method replaceBody to replace the values on the place holder. Check the sample code I am sharing so you can start somewhere.

Sample Code

code.gs

function onOpen() {
  DocumentApp.getUi()
  .createMenu("Sample Menu")
  .addItem("Summon Firm", "openForm")
  .addToUi();
}


function openForm() {
  const html = HtmlService.createHtmlOutputFromFile('index')
    .setWidth(300)
    .setHeight(150);
  DocumentApp.getUi().showModalDialog(html, 'Enter Your Name');
}

function insertInput(input) {
  const doc = DocumentApp.getActiveDocument();
  const body = doc.getBody();
  body.replaceText('{{NAME}}', input);
}

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>What is your name?</p>
    <input type="text" id="nameInput" placeholder="Enter name">
    <br><br>
    <button onclick="submitInput()">Submit</button>
    
    <script>
      function submitInput() {
        const name = document.getElementById('nameInput').value;
        google.script.run.withSuccessHandler(() => {
          google.script.host.close();
        }).insertInput(name);
      }
    </script>
  </body>
</html>

Sample Output

image

References:

Modal Dialog - Apps Script

createMenu - Apps Script

replaceText - Google Docs - Google Apps Script

Reasons:
  • Blacklisted phrase (1): What is your
  • Probably link only (1):
  • Long answer (-1):
  • Has code block (-0.5):
Posted by: Babanana