簡體   English   中英

如何在菜單按鈕上的不同文檔中運行 function

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

我正在嘗試運行腳本,因此當單擊菜單項時,會運行創建文本框的 function。 我不知道如何不只在谷歌應用程序腳本中拆分我的文檔並從菜單中運行 function。

我試圖將 function 放在菜單項中,但它與其他文檔無關。

菜單.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!');
}

文本框.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);
}

這個:

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 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
}

但不幸的是,您不能在菜單中使用帶有參數的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM