I have a current script in Google Docs that adds a header with collapsible list beneath it (on a pageless setup) underneath a specific section of the document.
I am looking for users to be able to trigger the script from within the document. I have been able to achieve this in Google Spreadsheets using links and Drawings (and using a custom menu, but that's less ideal in this instance), but have not been able to achieve the same thing within Google Docs.
Ideally, a user would click a link, which would trigger the script that adds the following:
Here's the script I have so far:
function insertText() {
var header = "Enter_Item_Title_Here"
var bulletOneHeader = "Agenda Item Owner(s): ___________"
var bulletTwoHeader = "Discussant: ___________"
var bulletThreeHeader = "Discussion Date: ___________"
var bulletFourHeader = "External follow up: ___________"
var bulletFiveHeader = "Notes: ___________"
var bulletSixHeader = "Action Items: ___________"
var cursor = DocumentApp.getActiveDocument().getActiveTab().asDocumentTab().getBody();
var pr = cursor.findText("New Items:").getElement().getParent();
var i = cursor.getChildIndex(pr) + 1;
cursor.insertParagraph(i, header).setHeading(DocumentApp.ParagraphHeading.HEADING3);
cursor.insertListItem(i + 1, bulletOneHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
cursor.insertListItem(i + 2, bulletTwoHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
cursor.insertListItem(i + 3, bulletThreeHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
cursor.insertListItem(i + 4, bulletFourHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
cursor.insertListItem(i + 5, bulletFiveHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
cursor.insertListItem(i + 6, bulletSixHeader).setGlyphType(DocumentApp.GlyphType.BULLET).setAttributes({ [DocumentApp.Attribute.BOLD]: true });
}
Here's an example google doc that includes the script I have so far.
I've been looking at and playing around with this existing question and also this one, but haven't been able to figure it out.
The goal is to trigger the script from within the document
using a link
.
If a custom menu
is less ideal in this instance
, this is an example script that uses Web Apps
, which should do what you'd like:
Template Text.gs
:
function doGet() {
return HtmlService.createHtmlOutputFromFile("Untitled");
}
Untitled.html
:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>You may now close this page.</p>
</body>
<script>
function myFunction() {
google.script.run.insertText();
}
window.onload = function() {
myFunction();
};
</script>
</html>
After adding these codes to your Template Text.gs
and Untitled.html
, select Deploy
> New deployment
> Next to "Select type," click Enable deployment types settings
> Enter the information about your web app in the fields under "Deployment configuration."
> Deploy
to get a link that you can embed to myLink
.
Note: This is simply a basic example. As I've mentioned in the comments, this
wouldn't be as straightforward as using a custom menu
since users will have to manually close the tab after theyclick
thelink
of the web app.