本文介绍了“遇到的错误:无效的参数”在处理函数中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理函数中遇到了这个错误,但我不知道是什么导致了它。我已经复制了代码并将其调试到非处理函数中,并且没有错误。

I'm getting this error in my handler function but I've no clue what's causing it. I've copied the code and debugged it in a non-handler function and there was no error.

function _responseToNext(e) {

  var app = UiApp.getActiveApplication();
  app.getElementById('btnPrev').setEnabled(true);

  var current = parseInt(CacheService.getPublicCache().get('currentItem'));
  var agendaItems = Utilities.jsonParse(CacheService.getPublicCache().get('agenda'));

  agendaItems[current]['notes'] = e.parameter.tAreaNotes;
  agendaItems[current]['status'] = e.parameter.lboxStatus;

  CacheService.getPublicCache().put('agenda', Utilities.jsonStringify(agendaItems));

  current = current + 1;
  CacheService.getPublicCache().put('currentItem', current);

  fillAgendaDetail(app);

  // only enabled 'Next' if there are more items in the agenda
  if (current < agendaItems.length-1) {
  app.getElementById('btnNext').setEnabled(true);
  }

  return app;
}


推荐答案

我想,错误原因在高速缓存为空的第一次执行期间,Cache get 方法返回null。 Utilities.jsonParse 会引发异常,并且缓存在任何情况下均为空。尝试使用以下经过修改的代码。

I suppose, the error cause is that the Cache get method returns null during the 1st execution when the cache is empty. The Utilities.jsonParse throws an exception and the cache becomes in any case empty. Try to use the following modified code.

function _responseToNext(e) {

  var app = UiApp.getActiveApplication();
  app.getElementById('btnPrev').setEnabled(true);

  var cachedCurrent = CacheService.getPublicCache().get('currentItem');
  var current;
  if (cachedCurrent == null) {
    current = 0;
  }
  else {
    current = parseInt(cachedCurrent);
  }
  var cachedAgendaItems = CacheService.getPublicCache().get('agenda');
  var agendaItems;
  if (cachedAgendaItems == null) {
    agendaItems = [][];
  }
  else {
    agendaItems = Utilities.jsonParse();
  }

  agendaItems[current]['notes'] = e.parameter.tAreaNotes;
  agendaItems[current]['status'] = e.parameter.lboxStatus;

  CacheService.getPublicCache().put('agenda', Utilities.jsonStringify(agendaItems));

  current = current + 1;
  CacheService.getPublicCache().put('currentItem', current);

  fillAgendaDetail(app);

  // only enabled 'Next' if there are more items in the agenda
  if (current < agendaItems.length-1) {
  app.getElementById('btnNext').setEnabled(true);
  }

  return app;
}

另外请提及Public Cache( CacheService。 getPublicCache())对于脚本的所有用户都是相同的。就你而言,这意味着,如果两个用户 [email protected] [email protected] 使用脚本它们将具有相同的当前 agendaItems 变量值,即它可以是 _responseToNext 处理程序已经在 user1 权限下执行 - current 变量等于1,在user2执行 _responseToNext 处理程序之后 - current 变量等于2,依此类推。如果您不需要这种行为,请使用 CacheService.getPrivateCache()

Also please mention that the Public Cache (CacheService.getPublicCache()) is the same for all users of your script. In your case, this means, if two users [email protected] and [email protected] use the script they will have the same current and agendaItems variables values, i.e. it can be a situation when the _responseToNext handler is already executed under the user1 authority - the current variable is equal to 1, after the user2 executes the _responseToNext handler - the current variable is equal to 2 and so on. If you do not need such behaviour, use the CacheService.getPrivateCache().

这篇关于“遇到的错误:无效的参数”在处理函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:29