作成したテキストエリアに
アクティブセルの値が入力される
Code.gs /************************************ メニューに追加する ************************************/ function onOpen() { SpreadsheetApp.getUi() .createMenu('SCRIPT') .addItem('GUI', 'openGUI') .addToUi(); } /************************************ UIを表示する ************************************/ function openGUI() { var html = HtmlService.createHtmlOutputFromFile('index') .setSandboxMode(HtmlService.SandboxMode.IFRAME); SpreadsheetApp.getUi() .showModalDialog(html, 'myGUI'); } /************************************ アクティブセルまでをグローバル化する ************************************/ var ss = SpreadsheetApp.getActive(); var sh = ss.getActiveSheet(); var range = sh.getActiveRange(); /************************************ アクティブセルの値を取得して返す ************************************/ function get_active_value(){ var value = range.getValue(); return value; } /************************************ 値をアクティブセルに入れる ************************************/ function set_active_value(value){ range.setValue(value); } /************************************ 上の行をアクティブにして値を取得して返す ************************************/ function get_prev_value(){ var col = range.getColumn(); var row = range.getRow()-1; sh.getRange(row , col).activate(); var value = sh.getRange(row , col).getValue(); return value; } /************************************ 下の行をアクティブにして値を取得して返す ************************************/ function get_next_value(){ var col = range.getColumn(); var row = range.getRow()+1; sh.getRange(row , col).activate(); var value = sh.getRange(row , col).getValue(); return value; }
index.html <!--/******************************** HTML ********************************/--> <textarea id="ta" onkeyup="run_input()"></textarea> <br> <input type="button" id="prev" value="prev" onclick="prev_bt()"> <input type="button" id="next" value="next" onclick="next_bt()"> <!--/******************************** JavaScript ********************************/--> <script> //UI起動時に実行するfunction load_active_value(); /******************************** load_active_value ********************************/ function load_active_value(){ google.script.run.withSuccessHandler(to_ta) .withUserObject(this) .get_active_value(); } function to_ta(value){//Code.gsのget_active_value()からvalueを受け取る document.getElementById("ta").value=value;//受け取ったvalueをtaに入れる } /******************************** run_input ********************************/ //taの値をCode.gsのset_active_value(value)に渡して実行する function run_input(){ var value = document.getElementById("ta").value; google.script.run.withUserObject(this).set_active_value(value); } /******************************** prev_bt ********************************/ function prev_bt(){ google.script.run.withSuccessHandler(prev_fnc) .withUserObject(this) .get_prev_value(); } function prev_fnc(value){//Code.gsのget_prev_value()からvalueを受け取る document.getElementById("ta").value=value;//受け取ったvalueをtaに入れる } /******************************** next_bt ********************************/ function next_bt(){ google.script.run.withSuccessHandler(next_fnc) .withUserObject(this) .get_next_value(); } function next_fnc(value){//Code.gsのget_next_value()からvalueを受け取る document.getElementById("ta").value=value;//受け取ったvalueをtaに入れる } </script>