简体   繁体   中英

How do I run a function in a differnent document on a menu button

I'm trying to run the script so when a menu item is clicked, a function that creates a text box is run. I can't figure out how not to only split up my documents in google app script and running a function from the menu.

I tried to place the function within the menu item, but it is not speaking to the other document.

menu.js.gs

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    // Or DocumentApp or FormApp.
    ui.createMenu('Custom Menu')
        .addItem('First item', 'menuItem1')
        .addSeparator()
        .addSubMenu(ui.createMenu('Sub-menu')
            function addTextBox(presentationId, pageId)
        .addToUi();
}

function menuItem1() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .alert('You clicked the first menu item!');
}

function menuItem2() {
    SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
        .alert('You clicked the second menu item!');
}

textbox.js.gs

/**
    * Add a new text box with text to a page.
    * @param {string} presentationId The presentation ID.
    * @param {string} pageId The page ID.
    */x
function addTextBox(presentationId, pageId) {
    // You can specify the ID to use for elements you create,
    // as long as the ID is unique.
    var pageElementId = Utilities.getUuid();

    var requests = [{
    'createShape': {
        'objectId': pageElementId,
        'shapeType': 'TEXT_BOX',
        'elementProperties': {
        'pageObjectId': pageId,
        'size': {
            'width': {
            'magnitude': 150,
            'unit': 'PT'
            },
            'height': {
            'magnitude': 50,
            'unit': 'PT'
            }
        },
        'transform': {
            'scaleX': 1,
            'scaleY': 1,
            'translateX': 200,
            'translateY': 100,
            'unit': 'PT'
        }
        }
    }
    }, {
    'insertText': {
        'objectId': pageElementId,
        'text': 'My Added Text Box',
        'insertionIndex': 0
    }
    }];
    var response =
        Slides.Presentations.batchUpdate({'requests': requests}, presentationId);
    Logger.log('Created Textbox with ID: ' +
        response.replies[0].createShape.objectId);
}

This:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Custom Menu')
      .addItem('First item', 'menuItem1')
      .addSeparator()
      .addSubMenu(ui.createMenu('Sub-menu')
            function addTextBox(presentationId, pageId)
      .addToUi();
}

Should be like this:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Custom Menu')
  .addItem('First item Title', 'menuItem1')
  .addSubMenu(SpreadsheetApp.getUi().createMenu("My SubMenu")
  .addItem('Add Text Box','addTextBox'))
  .addToUi();
  //With other functions below this
}

But unfortunately you can't use functions with parameters in menus.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM