“时出现错误:无效参数”,在处理函数(“Error Encountered: Invalid Arg

2019-10-17 04:28发布

我得到我的处理函数这个错误,但我一直不知道是什么导致了它。 我复制的代码,并在非处理函数调试,并没有错误。

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

Answer 1:

我想,错误的原因是缓存get方法时缓存为空第一次执行时返回null。 该Utilities.jsonParse抛出一个异常,并缓存在任何情况下空变。 尝试使用下面的修改后的代码。

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

同时,请市民缓存( CacheService.getPublicCache()是你的脚本的所有用户相同。 在你的情况,这意味着,如果两个用户user1@example.comuser2@example.com使用脚本,他们将有相同的currentagendaItems当变量的值,也就是说,它可以是一个情况_responseToNext处理器已经下执行user1权威机构- current变量等于1,用户2执行后_responseToNext处理程序-的current变量等于2,依此类推。 如果你不需要这样的行为,使用CacheService.getPrivateCache()



文章来源: “Error Encountered: Invalid Argument” in handler function