如何从提示或对话框中获取数据并通过 Google Web 应用程序脚本将其放入 Google 文档中
How do I get data from a prompt or dialog and put it in a Google Doc via Google Web App Script
我想用 Google 脚本创建一个 Google 文档,并根据用户输入为新文档命名。
随后的代码确实创建了新文档(Web 应用程序附加到新 Google 站点上的按钮)。现在我想添加从用户那里获取歌曲标题的代码。看了三天,我只是不知道该怎么做。
我能找到的最接近的代码是:write data in google sheet using a web script app
但它适用于 Google 表格,不适用于文档。
到目前为止,我创建文档的代码是:
Code.gs
function createNewLandscapeSong() {
var doc = DocumentApp.create('Rename with song title');
var title = "replace with song title"
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text1 = paragraph.appendText("© replace with writer(s)");
text1.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, title)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" value="Create New Song Doc"
onclick="google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong()" />
<script>
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
</body>
</html>
所以,我知道在这段代码的某处我需要添加提示用户输入歌曲标题的代码,然后我需要将其设置为变量并将其作为文档名称和标题插入歌曲(我的 "var doc" 和 "var title")。我只是不知道该怎么做。
根据 Sandy 的输入修改后的代码:
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function createNewLandscapeSong(objArgs) {
var docName = objArgs.docName;
var songTitle = objArgs.songTitle;
var songWriters = objArgs.songWriters;
Logger.log('title: ' + title)
var doc = DocumentApp.create(docName);
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text = paragraph.appendText(songWriters);
text.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, songTitle)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Fill in fields below to name Google Lyric Document and add the song title and writers.<br><br>
<input id="idNewDocName" type="text" placeholder="Google Doc Name"><br><br>
<input id="idNewSongTitle" type="text" placeholder="Song Title"><br><br>
<input id="idNewSongWriters" type="text" placeholder="Song Writers"><br><br>
<button onclick="saveUserInput()">Create New Lyric Doc</button>
<script>
window.saveUserInput = function() {
var docName = document.getElementById('idNewDocName').value;
var songTitle = document.getElementById('idNewSongTitle').value;
var songWriters = document.getElementById('idNewSongWriters').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong({docName:docName,songTitle:songTitle, songWriters: songWriters})
}
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
您需要几个输入字段。此示例显示了一种从输入字段中获取值的方法。这个例子没有使用表单标签,因为使用表单标签会导致浏览器选项卡在表单提交后被刷新,这会让人们感到困惑,而且通常不是他们想要的。
要调试代码,请在 Chrome 浏览器中按 f12 键打开开发人员工具。单击该按钮后,您应该会在控制台日志中看到歌曲标题的值。将数据作为 object 传递给服务器代码。在 运行 代码之后,从代码编辑器中单击“查看”菜单,然后选择“日志”以查看打印出来的日志。
这显示了一种从 Web 应用获取用户输入并将其传递到 Apps 脚本中的服务器代码的方法。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input id="idNewDocName" type="text" placeholder="New Doc Name">
<input id="idNewSongTitle" type="text" placeholder="New Song Title">
<button onclick="saveUserInput()">Create New Song Doc</button>
<script>
window.saveUserInput = function() {
var docName = document.getElementById('idNewDocName').value;
var songTitle = document.getElementById('idNewSongTitle').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong({docName:docName,songTitle:songTitle})
}
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
Code.gs
function createNewLandscapeSong(objArgs) {
var docName = objArgs.docName;
var title = objArgs.songTitle;
Logger.log('title: ' + title)
var doc = DocumentApp.create(docName);
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text1 = paragraph.appendText("© replace with writer(s)");
text1.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, title)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}
我想用 Google 脚本创建一个 Google 文档,并根据用户输入为新文档命名。
随后的代码确实创建了新文档(Web 应用程序附加到新 Google 站点上的按钮)。现在我想添加从用户那里获取歌曲标题的代码。看了三天,我只是不知道该怎么做。
我能找到的最接近的代码是:write data in google sheet using a web script app
但它适用于 Google 表格,不适用于文档。
到目前为止,我创建文档的代码是:
Code.gs
function createNewLandscapeSong() {
var doc = DocumentApp.create('Rename with song title');
var title = "replace with song title"
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text1 = paragraph.appendText("© replace with writer(s)");
text1.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, title)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" value="Create New Song Doc"
onclick="google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong()" />
<script>
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
</body>
</html>
所以,我知道在这段代码的某处我需要添加提示用户输入歌曲标题的代码,然后我需要将其设置为变量并将其作为文档名称和标题插入歌曲(我的 "var doc" 和 "var title")。我只是不知道该怎么做。
根据 Sandy 的输入修改后的代码:
Code.gs
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function createNewLandscapeSong(objArgs) {
var docName = objArgs.docName;
var songTitle = objArgs.songTitle;
var songWriters = objArgs.songWriters;
Logger.log('title: ' + title)
var doc = DocumentApp.create(docName);
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text = paragraph.appendText(songWriters);
text.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, songTitle)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}
Index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Fill in fields below to name Google Lyric Document and add the song title and writers.<br><br>
<input id="idNewDocName" type="text" placeholder="Google Doc Name"><br><br>
<input id="idNewSongTitle" type="text" placeholder="Song Title"><br><br>
<input id="idNewSongWriters" type="text" placeholder="Song Writers"><br><br>
<button onclick="saveUserInput()">Create New Lyric Doc</button>
<script>
window.saveUserInput = function() {
var docName = document.getElementById('idNewDocName').value;
var songTitle = document.getElementById('idNewSongTitle').value;
var songWriters = document.getElementById('idNewSongWriters').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong({docName:docName,songTitle:songTitle, songWriters: songWriters})
}
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
您需要几个输入字段。此示例显示了一种从输入字段中获取值的方法。这个例子没有使用表单标签,因为使用表单标签会导致浏览器选项卡在表单提交后被刷新,这会让人们感到困惑,而且通常不是他们想要的。
要调试代码,请在 Chrome 浏览器中按 f12 键打开开发人员工具。单击该按钮后,您应该会在控制台日志中看到歌曲标题的值。将数据作为 object 传递给服务器代码。在 运行 代码之后,从代码编辑器中单击“查看”菜单,然后选择“日志”以查看打印出来的日志。
这显示了一种从 Web 应用获取用户输入并将其传递到 Apps 脚本中的服务器代码的方法。
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input id="idNewDocName" type="text" placeholder="New Doc Name">
<input id="idNewSongTitle" type="text" placeholder="New Song Title">
<button onclick="saveUserInput()">Create New Song Doc</button>
<script>
window.saveUserInput = function() {
var docName = document.getElementById('idNewDocName').value;
var songTitle = document.getElementById('idNewSongTitle').value;
console.log('songTitle: ' + songTitle)
google.script.run
.withSuccessHandler(openNewDoc)
.createNewLandscapeSong({docName:docName,songTitle:songTitle})
}
function openNewDoc(results){
window.open(results.url, '_blank').focus();
}
</script>
</body>
</html>
Code.gs
function createNewLandscapeSong(objArgs) {
var docName = objArgs.docName;
var title = objArgs.songTitle;
Logger.log('title: ' + title)
var doc = DocumentApp.create(docName);
var url = doc.getUrl();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, "");
var text1 = paragraph.appendText("© replace with writer(s)");
text1.setFontSize(8);
var rowsData = [['PUT FIRST VERSE/CHORUS HERE.', 'PUT SECOND VERSE/NEXT CHORUS/BRIDGE/ETC HERE.']];
var style = {};
body.insertParagraph(0, title)
.setHeading(DocumentApp.ParagraphHeading.HEADING3);
table = body.appendTable(rowsData);
style[DocumentApp.Attribute.BORDER_WIDTH] = 0;
table.setAttributes(style);
return {
url: url,
title: title
};
}