Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2020年8月15日土曜日

localStorageのkeyをすべて取得したい


MISSION 
localStorageのkeyをすべて取得する


KEY
Object.keys(localStorage)
でlocalStorageのkeyを取得できる



PROCESS

コード.gsとindex.htmlで、こういうUIを作って試してみる

  1. 入力ボックスに値を入れる
  2. setをクリックする
    • localStorageに {値:日時} を保存する
  3. getKeysをクリックする
    • console.logに [値, 日時] を出力する
  4. localStorageから削除したい値を入れる
  5. deleteKeysをクリックする
    • 入力した値のデータ(プロパティ)をlocalStorageから削除する



コード.gs 
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html 
<!DOCTYPE html>
<html>
<body>
  <input id="tb" type="text">
  <button id="set">set</button>
  <button id="getKeys">getKeys</button>
  <button id="deleteKeys">deleteStorage</button>
  
<script>
elem("set").onclick = setClicked;
elem("getKeys").onclick = getkeys;
elem("deleteKeys").onclick = deleteStorage;

function elem(id) {
  return document.getElementById(id);
}

function setClicked() {
  const key = elem("tb").value;
  const datetime = new Date();
  setStorage(key, datetime);
  elem("tb").value = "";
  console.log(key + " set");
}

function setStorage(key, value) {
  const item = localStorage.setItem(key, value);
}

function getkeys() {
  const keys = Object.keys(localStorage);
  for(let i = 0; i < localStorage.length; i++) {
    const key = keys[i];
    const datetime = localStorage.getItem(key)
    console.log([key, datetime]);
  }
}

function deleteStorage() {
  const key = elem("tb").value;
  localStorage.removeItem(key);
  console.log(key + " deleted");
}

</script>
</body>
</html>

Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...