英文:
reading file contents of container bound script
问题
我试图执行一个作为菜单插件的一部分的模板(通过SpreadsheetApp.getUi().createMenu),并将任意文件内容附加为脚本或样式
```javascript
function requireGs(scripts) {
console.log(scripts)
return `<script>\n${scripts
.map(function (d) {
const text = ScriptApp.getResource(d).getDataAsString()
return text
})
.join('\n\n')}</script>\n`
}
function requireCSS(scripts) {
return `<style>\n${scripts
.map(function (d) {
return ScriptApp.getResource(d).getDataAsString()
})
.join('\n\n')}</style>\n`
}
它将被用于:
<?!= requireGs(['server/models.gs']); ?>
<?!= requireCSS(['client/stylesheet.html']); ?>
然而,ScriptApp.getResource
不存在,我在文档中找不到它,也找不到替代品。
2023年5月17日,下午6:56:24 调试 [ 'server/models.gs' ]
2023年5月17日,下午6:56:24 错误 异常:未找到
如何访问推送到容器绑定脚本项目的文件内容?
<details>
<summary>英文:</summary>
I'm trying to execute a template that is part of a menu addon (via SpreadsheetApp.getUi().createMenu) with arbitrary file contents appended as scripts or styles
function requireGs(scripts) {
console.log(scripts)
return <script>\n${scripts
.map(function (d) {
const text = ScriptApp.getResource(d).getDataAsString()
return text
})
.join('\n\n')}</script>\n
}
function requireCSS(scripts) {
return <style>\n${scripts
.map(function (d) {
return ScriptApp.getResource(d).getDataAsString()
})
.join('\n\n')}</style>\n
}
which would be used as
<?!= requireGs(['server/models.gs']); ?>
<?!= requireCSS(['client/stylesheet.html']); ?>
however, `ScriptApp.getResource` doesn't exist, and I can't see it in the docs nor can I find a replacement for it.
May 17, 2023, 6:56:24 PM Debug [ 'server/models.gs' ]
May 17, 2023, 6:56:24 PM Error Exception: Not found
How can I access contents of files pushed to a container bound script project?
</details>
# 答案1
**得分**: 3
显示项目中所有文件和函数
```javascript
function displayAllScriptFilesAndFunctions() {
var scriptId;
const idresp = SpreadsheetApp.getUi().prompt("ScriptId", "输入脚本 ID", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
if (idresp.getSelectedButton() == SpreadsheetApp.getUi().Button.OK) {
if (idresp.getResponseText().length == 0) {
scriptId = ScriptApp.getScriptId();
} else {
scriptId = idresp.getResponseText();
}
const url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";
const options = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
const res = UrlFetchApp.fetch(url, options);
let data = JSON.parse(res.getContentText());
let uA = [];
let dA = [];
const files = data.files;
let html = `<strong>文件和函数</strong><br />Id:${scriptId}<hr>`;
files.forEach(file => {
html += Utilities.formatString('<br /><strong>文件名:</strong> %s<br /><strong>类型:</strong> %s', file.name, file.type);
if (file.functionSet && file.functionSet.values && file.functionSet.values.length > 0) {
html += Utilities.formatString('<br /><strong> 函数名:</strong>')
file.functionSet.values.forEach((func, i) => {
if (!~uA.indexOf(func.name)) { uA.push(func.name); } else { dA.push(func.name); }
html += Utilities.formatString('<br /> %s', func.name)
});
} else {
html += '<br /><strong>未列出函数。</strong>';
}
html += '<hr>';
});
if (dA.length > 0) {
html += Utilities.formatString('<br><strong>重复函数</strong><hr><br>%s<hr>', dA.join('<br>'))
}
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600), '文件和函数');
}
}
英文:
All files and functions in a project
function displayAllScriptFilesAndFunctions() {
var scriptId;
const idresp = SpreadsheetApp.getUi().prompt("ScriptId", "Enter Script Id", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
if (idresp.getSelectedButton() == SpreadsheetApp.getUi().Button.OK) {
if (idresp.getResponseText().length == 0) {
scriptId = ScriptApp.getScriptId();
} else {
scriptId = idresp.getResponseText();
}
const url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";
const options = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
const res = UrlFetchApp.fetch(url, options);
let data = JSON.parse(res.getContentText());
//SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput('<textarea rows="50" cols="120"/>' + res + '</textarea>').setWidth(1000).setHeight(500), 'Return');
let uA = [];
let dA = [];
const files = data.files;
let html = `<strong>Files and Functions</strong><br />Id:${scriptId}<hr>`;
files.forEach(file => {
html += Utilities.formatString('<br /><strong>FileName:</strong> %s<br /><strong>Type:</strong> %s', file.name, file.type);
if (file.functionSet && file.functionSet.values && file.functionSet.values.length > 0) {
html += Utilities.formatString('<br /><strong>&nbsp;&nbsp;&nbsp;&nbsp;Function Names:</strong>')
file.functionSet.values.forEach((func, i) => {
if (!~uA.indexOf(func.name)) { uA.push(func.name); } else { dA.push(func.name); }
html += Utilities.formatString('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s', func.name)
});
} else {
html += '<br /><strong>No functions listed.</strong>';
}
html += '<hr>';
});
if (dA.length > 0) {
html += Utilities.formatString('<br><strong>Duplicate Functions</strong><hr><br>%s<hr>', dA.join('<br>'))
}
SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600), 'Files and Functions');
}
}
答案2
得分: 0
文件扩展名似乎必须被移除才能使其工作。ScriptApp.getResource是未记录的,但目前可用。
英文:
It seems like file extensions have to be removed for it to work. ScriptApp.getResource is undocumented but it works as of now.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论