1

我的处理程序函数中出现此错误,但我不知道是什么原因造成的。我已经复制了代码并在非处理程序函数中对其进行了调试,并且没有错误。

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

1 回答 1

0

我想,错误原因是get当缓存为空时,Cache 方法在第一次执行期间返回 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在 user2 执行后变量等于 1处理_responseToNext程序 -current变量等于 2,依此类推。如果您不需要这种行为,请使用CacheService.getPrivateCache().

于 2012-08-29T06:13:21.837 回答