在Google Apps Script (GAS) 项目中为Google Doc Add-on获取namedRanges,您需要遵循以下步骤:
index.html
。这个文件将包含您的Add-on的用户界面。<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function listNamedRanges() {
google.script.run.withSuccessHandler(onSuccess).listNamedRanges();
}
function onSuccess(namedRanges) {
console.log(namedRanges);
}
</script>
</head>
<body>
<button onclick="listNamedRanges()">列出命名范围</button>
</body>
</html>
code.gs
。这个文件将包含与Google Doc交互的逻辑。function listNamedRanges() {
var doc = DocumentApp.getActiveDocument();
var namedRanges = doc.getNamedRanges();
var result = [];
for (var i = 0; i < namedRanges.length; i++) {
var namedRange = namedRanges[i];
result.push({
name: namedRange.getName(),
range: namedRange.getRange()
});
}
return result;
}
appsscript.json
文件,然后添加以下内容以配置您的Add-on:{
"timeZone": "America/New_York",
"dependencies": {
"enabledAdvancedServices": [{
"userSymbol": "DocumentApp",
"version": "v1",
"serviceId": "document"
}]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
listNamedRanges
函数,并在控制台中显示文档中的命名范围。
领取专属 10元无门槛券
手把手带您无忧上云