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

2021年5月4日火曜日

Google Apps ScriptでURLの表示を変更したい google.script.history.replace()


google.script.history.replace()

で実現できました。


こういうことを実現したい

HtmlServiceのWebアプリのURL
https://script.google.com/macros/s/ID/exec

ページ遷移を発生させずパラメーターを追加したい
https://script.google.com/macros/s/ID/exec?id=1&value=hello



URLの表示を変更したくて調べてみると、History API を取り扱う が見つかりました。
試してみると、history.pushState() や history.replaceState() では変更できなかった。

さらに調べてみると Class google.script.history (Client-side API) が見つかりました。

google.script.history.push() と google.script.history.replace() で実現できました。
pushとreplaceの違いはリファレンスを参照してください。

今回は replace() を使ったコードを書きました。



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




index.html
<!DOCTYPE html>
<html>
<body>

<script>

replaceParam();
function replaceParam() {
  var params = {
    "id": 1,
    "value": "hello"
  };
  google.script.history.replace("", params, "");
}
</script>

</body>
</html>




参考

Class google.script.history (Client-side API) 

History API を取り扱う


Latest post

Googleドキュメントに見出しを追加したい

今回の例では、ドキュメントの末尾に「見出しD」 を追加します。 見出しA, B, C, Dのスタイルは、見出し3 ( HEADING3 ) に設定しています。  下記Code.gsの  GOOGLE_DOCUMENT_URL を設定して  addHeadingToEnd()  を...