将其他参数传递给Apps脚本事件对象 [英] Passing additional parameters to an Apps Script event object

查看:72
本文介绍了将其他参数传递给Apps脚本事件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Apps脚本中的回调和作用域感到头疼。我具有以下功能,该功能会为当前打开的电子邮件中的每个附加文件创建一个带有复选框的Gmail加载项部分:

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

所有内容均按预期显示,但我希望用户也能够选择他要取消选中以供以后处理的文件。
在我的checkboxAltered函数中,我想处理用户选择输入操作,这是唯一可行的方法(据我从文档中了解到的那样。请告诉我我是否错!)。当前它是一个空函数,仅用于检查我的理解程度:

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 ,事件对象,此处始终未定义选择(一个预先初始化并检查过的)全局布尔数组,每个附件都有一个项目,总是简单地 []

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.

我在做什么错?
如何使用setParameters(Object)告诉回调函数内部用户单击了哪个复选框?
是否不能从回调函数访问全局变量?

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?

推荐答案

查看CardService文档中的 Action 类的< a href = https://developers.google.com/apps-script/reference/card-service/action#setParameters(Object) rel = nofollow noreferrer> setParameters() 方法。如果要将自定义参数传递给回调,则需要使用该方法。

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.

这篇关于将其他参数传递给Apps脚本事件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆