Passing additional parameters to an Apps Script event object

牧云@^-^@ 提交于 2020-01-15 10:44:26

问题


I'm having a lot of headaches with callbacks and scopes in Apps Script. I have the following function, that creates a Gmail Add-On section with a checkbox for each attached file on the currently open email:

function createAttachmentsSection( attachments ){

  var widget = CardService.newSelectionInput()
    .setFieldName( 'selectedFiles' )
    .setType( CardService.SelectionInputType.CHECK_BOX )
    .setOnChangeAction(
      CardService.newAction()
        .setFunctionName( 'checkboxAltered' )
    );

  for ( var i = 0 ; i < attachments.length; i++ )
    widget.addItem( attachments[i].getName(), '' + i ,  true )

  return CardService.newCardSection().addWidget( widget );
}

Everything is displayed as expected, but I want the user to be also able to select which files he wants to uncheck for later processing. In my checkboxAltered function I want to handle user selection input actions, which is the only way to go (as far as I've learned from the documentation. Please tell me if I'm wrong!). It's currently an empty function, only used to check how little I understand:

function checkboxAltered( e ){

  Logger.log( e );
  Logger.log( selections.length );
}

e, the event object, is always undefined here. And selections, a (previously initialized and checked) global bool array with an item for each attachment, is always simply [] inside checkboxAltered.

What am I doing wrong? How can I use setParameters( Object ) to tell inside the callback function which checkbox was clicked by the user? Are global variables not accessible from callback functions?


回答1:


Looking at the CardService documentation the Action class has a setParameters() method. If you want to pass custom parameters to the callback you'll need to use that method.



来源:https://stackoverflow.com/questions/48733462/passing-additional-parameters-to-an-apps-script-event-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!