这个解释表明:
- 自动创建自定义菜单项
- 打开对话框的代码
<script>
对话框的HTML 和HTML 中的标记
- 将结果放入电子表格的代码
Code.gs - onOpen() - 创建自定义菜单
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Custom Menu')
.addItem('Show Upload Dialog', 'showUploadBox')
.addToUi();
};
gs 代码 - 打开对话框
function showUploadBox() {
var htmlOutput = HtmlService.createHtmlOutputFromFile('Dialog')
.setWidth(500)
.setHeight(500);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Title of Dialog');
};
对话框上传.html
使用此 HTML 创建一个新文件
<div id="formDiv">
<form id="myForm">
<input id="fileName" name="picToLoad" type="file" />
<br>
<br/>
<input type="button" value="Submit" onclick="fncGetFileName()" />
<br>
<br>
<br>
<input id="idBtnClose" type="button" value="Close" style="display: none" onclick="google.script.host.close()" />
</form>
</div>
<br>
<br>
<div id="status" style="display: none">
<!-- div will be filled with innerHTML after form submission. -->
Working. Please wait...
</div>
<script>
function fncGetFileName(frmData) {
console.log('fncGetFileName ran!');
var theFileName = document.getElementById('fileName').value;
theFileName = theFileName.slice(12);
console.log('theFileName: ' + theFileName);
document.getElementById('status').style.display = 'inline'; //Display msg
google.script.run
.withSuccessHandler(updateOutput)
.processForm(theFileName)
};
// Javascript function called by "submit" button handler,
// to show results.
function updateOutput() {
var outputDiv = document.getElementById('status');
outputDiv.innerHTML = "The File Name was Written!";
document.getElementById('idBtnClose').style.display = 'inline'; //Display msg
}
</script>
将文件名保存到工作表的 gs 代码
function processForm(argFileName) {
Logger.log('argFileName: ' + argFileName);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var theSheet = ss.getActiveSheet();
var theRange = theSheet.getRange("B4");
theRange.setValue(argFileName);
};