私はこの問題を抱えています...私はドキュメントを読んでいますが、できません。サイドバーのhtmlフォームからgoogleスクリプトに値を渡す
私はGoogleドキュメントのサイドバーに小さなHTMLフォームを作成したいと考えています。 このフォームで連絡先グループが連絡先からプルされ、ユーザーはこれを選択してスクリプトに渡すことができます。
これは私がやったことです:私は上をクリックすると
Page.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Hello, world! <input type="button" value="Close" onclick="google.script.host.close()" />
</body>
</html>
menu.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Page')
.evaluate();
}
function onOpen() {
DocumentApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Custom Menu')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Page')
.setTitle('Chose the Contact Group')
.setWidth(300);
var groups = ContactsApp.getContactGroups();
// add this to the html file Page.html
html.append ('<form onsubmit="google.script.run.myFunction(formObject)">'+
'<select>');
for (var i = 0; i < groups.length; i++) {
html.append('<option name="chosenGroup" value="' + groups[i].getName() + '">' + groups[i].getName() + '</option>');
}
html.append('</select><input type="submit">');
//this is commented because I was testing it
//html.append("<script>function handleFormSubmit(formObject) { google.script.run.myFunction(formObject); } </script>");
DocumentApp.getUi() // Or DocumentApp or FormApp.
.showSidebar(html);
}
Code.gs
myFunction (formObject) {
Logger.log(formObject.chosenGroup);
}
を送信ボタンを押すと新しい空白ページが開き、次のようなURL:
https://n-wuewuewuee98efgdsf98769s8d76f9s76df-0lu-script.googleusercontent.com/userCodeAppPanel?
ありがとうございましたAntonさん、それは動作していますが、私はここでjqueryが必要な理由を理解できません。「google.script.run.processFormResponse(this)」を追加することはできません。オンリックで?私はこのページを誤解しているかもしれません:https://developers.google.com/apps-script/guides/html/communication#forms(ウェブアプリケーションのためのフォームスクリプトです。) – Kintaro
更新された答えをご覧ください。 –