首页 > 解决方案 > 谷歌应用脚​​本 CacheService 奇怪的行为

问题描述

我使用应用程序脚本为谷歌驱动器做了一个简单的添加。它计算点击文件的次数。代码片段:

let onHomepage = (e) => {
  return createHomeCard();
};

let createHomeCard = (text = "") => {
  var contextText = CardService.newTextParagraph()
    .setText("<b>Click on a file to start</b>");
  var tapsText = CardService.newTextParagraph()
    .setText(text);

  // Assemble the widgets and return the card.
  var section = CardService.newCardSection()
    .addWidget(contextText)
    .addWidget(tapsText);

  var card = CardService.newCardBuilder()
    .addSection(section);

  return card.build();
};

let onDriveItemsSelected = (e) => {
  updateCount();
  return createHomeCard("count : " + getCount());
};

let updateCount = () => {
  let count = parseInt(CacheService.getUserCache().get("count"));
  if (!count){
    count = 0;
  }
  count += 1;
  CacheService.getUserCache().put("count", count + "");
};

let getCount = () => {
  return CacheService.getUserCache().get("count");
};

每次点击都是一个简单的计数+=1。

但是,如果我的驱动器只有 3 个文件,我不能让它通过 3,正如您在下面附加的视频中看到的那样,如果我点击第一个文件,计数将回到 1。

我错过了什么?

在这个片段中,我使用“CacheService”来存储和检索缓存,但我也尝试过“PropertiesService”并且都给了我相同的结果。

请指教。

谢谢。

视频:https ://youtu.be/uWR5lVdGViU

标签: google-apps-script

解决方案


推荐阅读