コード.gs
function openGUI() { var html = HtmlService.createHtmlOutputFromFile('index'); SpreadsheetApp.getUi().showModalDialog(html, 'myGUI'); } function set_values_gs(value) { var ss = SpreadsheetApp.getActiveSpreadsheet(); var sh = ss.getActiveSheet(); var last_row = get_last_data_row(ss, sh); sh.getRange("A" + last_row).setValue(value); } function get_last_data_row(ss, sh) { var values = sh.getRange("A:A").getValues(); for (var i = values.length - 1; i >= 0; i--) { if (values[i] != "") { break; } } var last_row = i + 2; return last_row; }
index.html
<!DOCTYPE html> <html> <body> <textarea id="ta"></textarea> <script> document.getElementById("ta").focus(); document.getElementById("ta").onkeyup = ta_keyup; function ta_keyup(e) { var key_code = e.keyCode; var value = this.value; var value_split = value.split("\n"); if (key_code == 13) { //returnキーなら if (value_split[value_split.length - 2] == "") { //改行2つなら google.script.run.withSuccessHandler(onSuccess) .withFailureHandler(onFailure) .set_values_gs(value); } } else {} } function onSuccess() { document.getElementById("ta").value = ""; } function onFailure(e) { alert([e.message, e.stack]); } </script> </body> </html>
スプレッドシートに表示される画面