我有一个运行良好的Web应用程序,并创建了一个Google文档。该应用程序安装在新的Google网站上的按钮中。有时,我需要从网站创建一个新文档,但是有时候我需要在打开Google Spreadsheet(该表单包含与网站相关的数据并且嵌入到网站中)时创建一个新文档。
我在该工作表上有一个自定义菜单,可以执行许多不同的操作。我想在该菜单中添加一个项目,以访问上述Web应用程序并运行它。
首先:这可能吗?
第二,我假设如果可能,那么我必须使用其URL来调用Web App脚本(就像我对Google网站上附加的按钮所做的那样)。
我的Web应用代码如下:
function doGet() {
return HtmlService
.createTemplateFromFile('Index')
.evaluate();
}
function createNewLandscapeSong(objArgs) {
var docName = objArgs.docName;
var songTitle = objArgs.songTitle;
var songWriters = objArgs.songWriters;
Logger.log('songTitle: ' + songTitle)
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,
songTitle: songTitle
};
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Fill in fields below to name Google Lyric Document<br>
and add the song title and writers.<br>
Then click button to create new song lyric docunent.<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>
/*This menu in use.
This function creates the custom menu. Note: Line 3 throws error, but this script works.*/
function onOpen() {
//SpreadsheetApp.getActiveSpreadsheet().toast('Task started');
var ui = SpreadsheetApp.getUi();
ui.createMenu('Songpower')
.addItem('Website', 'openSite')
.addItem('Open Google Drive', 'openDrive')
.addItem('Open Old SP Site', 'openOldSP')
.addItem('Create', 'openCreate')
.addItem('Perform', 'openPerform')
.addItem('Catalog', 'openCatalog')
.addItem('New Lyric - Landscape', 'createLandscapeLyricDoc')
.addItem('New Lyric - Landscape Test', 'newLyricTest')
.addItem('New Lyric - Portrait', 'openPortrait')
.addItem('Add Song to Catalog', 'addSong')
.addToUi();
}
function openSite() {
var selection = SpreadsheetApp.getActiveSheet();
var html = "<a href='https://sites.google.com/view/sp-site/catalog'; target='_blank'>Open Songpower</a>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Songpower');
}
function openDrive() {
var selection = SpreadsheetApp.getActiveSheet();
var html = "<a href='https://drive.google.com/drive/my-drive'; target='_blank'>Open My Google Drive</a>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open My Google Drive');
}
function openOldSP() {
var selection = SpreadsheetApp.getActiveSheet();
var html = "<a href=url'; target='_blank'>Open Old Songpower Site</a>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open My Google Drive');
}
function openCreate() {
var selection = SpreadsheetApp.getActiveSheet();
var html = "<a href='url'; target='_blank'>Open Create Spreadsheet</a>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Create Sheet');
}
function openPerform() {
var selection = SpreadsheetApp.getActiveSheet();
var html = "<a href= 'url'; target='_blank'>Open Perform Spreadsheet</a>";
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Perform Sheet');
}
function createLandscapeLyricDoc() {
var doc = DocumentApp.create('Rename');
var title = "replace with song title and then link this text to song title cell in Catalog Spreadsheet"
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. (SUGGEST USE ALL CAPS.)', '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);
var html = '<a href= "' + url + '"; target="_blank">Open new lyric doc</a>;'
var selection = SpreadsheetApp.getActiveSheet();
var userInterface = HtmlService.createHtmlOutput(html);
/*Note: The following line throws error when you debug,
but the script works from the Catalog Sheet Songpower Menu.*/
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Landscape New Lyric Doc');
}
function newLyricTest() {
// This is where I want to put call to run the SPSiteNewSongButtonScript (Web App)
}
function openPortrait() {
var html = "<a href= 'url'; target='_blank'>Open 1-Column Lyric Template</a>";
var selection = SpreadsheetApp.getActiveSheet();
var userInterface = HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModalDialog(userInterface, 'Open Portrait New Lyric Doc');
}
function addSong() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = Browser.inputBox('New Song', 'Enter the song title', Browser.Buttons.OK_CANCEL);
var namedRange = sheet.getRange("Title");
var range=sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
range.setValue(songTitle);
SpreadsheetApp.setActiveRange(range);
}
答案 0 :(得分:0)
有几种方法可以解决这个问题,
1)使用sidebar或custom dialogs在Google电子表格中呈现HTML页面。它的行为就像电子表格中的webapp。
伪代码:
首先在电子表格(html和code.gs)中复制Web应用程序,然后修改newLyricTest()
function newLyricTest()
{
var html = HtmlService.createHtmlOutputFromFile('Index')
.setTitle('My custom sidebar')
.setWidth(300);
SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
.showSidebar(html);
}
运行上面的代码时,html页面的值将呈现在电子表格的一侧,您可以在其中输入值并运行保存文件。
2)您只是提示我们获取docName,songTitle和songWriters的值。 伪代码:
function newLyricTest()
{
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
'New Lyric Test',
'Please enter Document name:',
ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
if (button == ui.Button.OK) {
// User clicked "OK".
var docName = text
} else if (button == ui.Button.CANCEL) {
// User clicked "Cancel".
return // exit function
} else if (button == ui.Button.CLOSE) {
// User clicked X in the title bar.
return // exit function
}
// Now repeat the process as above to get values for songTitle and songWriters.
// once you have the values of all these variables you can run the remaining code
// of function createNewLandscapeSong()
Logger.log('songTitle: ' + songTitle)
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,
songTitle: songTitle
};
}
以上代码是从Web应用程序修改的代码副本。我已经给出了使用提示来获取docName值的示例,您可以类似地获取songTitle和songWriters的值。获得这些值后,您可以简单地从Web应用程序中运行其余代码。
注意:这绝不是解决此问题的详尽方法列表。另外,请注意,这两种方法都依赖于在电子表格脚本编辑器中拥有Web应用程序代码的副本。最后,以上所有代码均为伪代码,未经测试。代码是从上面列出的边栏中的示例和提示文档中复制的。