LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Tuesday, May 4, 2021

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

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...