0

I am trying to make a chrome extension that adds an extra custom menu to google docs. I have already figured out how to make an extension, and I also have figured out how to make an extra menu in apps script. I just can't figure out how to run the apps script code from the extension. I know that similar questions have been asked, but I am too inexperienced to understand those answers.

Here is my apps script code:

function onOpen() {
  const ui = DocumentApp.getUi();
  // Creates the extra insert menu
  ui.createMenu('Insert Tool')
      // .addItem adds a button to the new menu
      .addItem('Insert Rickroll', 'insertRickroll')
      .addToUi();
}
 
function insertRickroll() {  
  // Gets the cursor position
  const cursor = DocumentApp.getActiveDocument().getCursor();
  // Inserts link text at position
  cursor.insertText('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
   
}

My chrome extension, as of now, just shows some text. It does not run any scripts. Can anybody help me combine the two?

I have tried directly pasting the code into a scripts as part of the extension, but it ended up not working to add the menu in docs. This could have been a result of not actually having anything happen in the extension's script and HTML code other than that copy and paste.

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Keep in mind that your extension code runs in the browser, and Google apps scripts run on a server in the Google cloud. In your extension you'd need to simulate UI interactions like puppeteer does, e.g. `open menu 'Insert Tool'` => `click on 'Insert Rickroll'` – Peter Thoeny Nov 11 '22 at 23:13
  • This tutorial manipulates Google Sheet with Google Apps Script. [How to make Chrome Extension 52 Google Sheets](https://youtu.be/oENQJLovkwY) – Norio Yamamoto Nov 12 '22 at 00:13

1 Answers1

1

To run Apps Script code from a web browser extension use Google Apps Script API.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166