How can I add a custom menu in Google Docs via Apps Script?

Using Apps Script, I tried adding an ‘Automated Sharing’ option to Google Docs, but it never appears. How can I enable it?

function docStart() {
  const uiInstance = DocumentApp.getUi();
  uiInstance.createMenu('Rapid Share')
    .addItem('Share Now', 'executeShare')
    .addToUi();
}

In my experience, make sure that the function adding the custom menu executes every time the document is opened. I ran into a similar issue when I named my function something other than onOpen. Changing it to onOpen ensured that the menu was automatically loaded. It is also important to ensure that the script is correctly bound to your document and that necessary permissions are granted. Logging details and checking for potential errors in the script editor sometimes reveal subtle issues related to execution context. A careful review and proper initialization usually resolves the issue.

The challenge you’re facing can sometimes be due to how the script is integrated with the document rather than the code itself. In my experience, after binding the script properly, I had to force device reauthorization after making significant changes to the script. It was also crucial to ensure that the onOpen function was not being blocked by other errors or conflicting triggers. I eventually resolved the issue by manually refreshing the document, which helped update the UI to reflect the new menu option. Consistent verification of permissions and reloading the document are key.

hey, i fixed it by renaming the function to onOpen and waiting a bit before running the script. sometimes the script needs a manual trigger too. you might try a full reload of the doc to see if that helps.

I experienced a similar issue when adding a custom menu in a container-bound script. In my case, re-checking the script’s binding to the document was essential since I accidentally worked in a standalone project at one point. After confirming the proper association, I also reauthorized the script and manually refreshed the document to witness the changes. Monitoring the error logs in the Apps Script editor helped identify any permission or trigger-related issues that could be hindering the onOpen function from executing as expected.

I encountered a similar issue and eventually found out that my custom menu wasn’t appearing because I was inadvertently editing an unbound script instead of the document’s container-bound project. It’s crucial to double-check that the script is correctly bound to your document. In my case, I also discovered that the new menu options wouldn’t take effect until I saved the script and waited a few seconds for the changes to propagate. Adding concise logging inside the onOpen function helped confirm that everything was running as expected. I recommend verifying the binding and ensuring that your changes are saved and properly published.