Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Google SpreadSheetのデータ入力支援サイドバーをつくってみる その2:更新時に値を変更する

code.gs

function onOpen() {
  //サイドメニュー表示
  SpreadsheetApp.getUi() 
      .createMenu('Custom Menu')
      .addItem('Show sidebar', 'showSidebar')
      .addToUi();
}

function changeVal(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var cell = ss.getSheets()[0].getRange('B5');
  return cell.setValue("sample value, coming here!");
}

function showSidebar() {
  //サイドバー
  var html = HtmlService.createHtmlOutputFromFile('page')
      .setTitle('My custom sidebar')
      .setWidth(300); //幅300px
  SpreadsheetApp.getUi() 
      .showSidebar(html);
  changeVal();
}

page.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <p>Hello, world!</p>
    <hr/>
    <input type="button" value="Close" onclick="google.script.host.close()" />
  </body>

</html>

こんな感じに

f:id:shuzo_kino:20180904232356p:plain