LANG SELRCT

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

Sunday, February 16, 2020

Google Apps ScriptのHtml ServiceでスクリプトのURLを返したい


ScriptApp.getScriptId()
でスクリプトIDを取得できるのでそれを利用します。


スクリプトのアドレスバーを見てみるとこうなっています。
https://script.google.com/d/Script ID/edit?usp=drivesdk


Script IDの位置にScriptApp.getScriptId()で取得した値を入れてHtml側に返します。


今回のコード.gsでは、スクリプトファイルをHtml側から開けるようにしています。



コード.gs
function doGet(e) {
  return HtmlService.createHtmlOutputFromFile("index");
}

function returnScriptUrl(){
  var scriptId = ScriptApp.getScriptId();
  var scriptUrl = "https://script.google.com/d/" + scriptId + "/edit?usp=drivesdk";
  return scriptUrl;
}




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

  <a id="scriptUrlLink" target="_blank">Script URL</a>

<script>

getScriptUrl();

function getScriptUrl() {
  google.script.run
  .withFailureHandler(onFailure)
  .withSuccessHandler(onSuccess)
  .returnScriptUrl();
}

function onSuccess(scriptUrl) {
  document.getElementById("scriptUrlLink").href = scriptUrl;
}

function onFailure(e) {
  alert([e.message, e.stack]);
}

</script>

  </body>
</html>




Latest post

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

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