问题
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