为了明确起见,我们正在打开一个谷歌文档文件,然后进入脚本编辑器。
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Add TimeStamp" onClick="google.script.run.addTimeStamp()" style="width:250px;height:150px;text-align:center;margin:10px 0 10px 0;vertical-align:top;" />
</body>
</html>
然后将以下代码放入代码中。gs文件:
function onOpen()
{
DocumentApp.getUi().createMenu('My Menu')
.addItem('Show Sidebar','showSideBar' )
.addToUi();
}
function addTimeStamp()
{
var ts=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), 'MM/dd/yyyy HH:mm:ss');
DocumentApp.getActiveDocument().getBody().appendParagraph(ts);
}
function showSideBar()
{
var ui=HtmlService.createHtmlOutputFromFile('sidebar');
DocumentApp.getUi().showSidebar(ui);
}
function doGet()
{
var ui=HtmlService.createHtmlOutputFromFile('sidebar');
return ui.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
}