读取容器绑定脚本的文件内容。

huangapple go评论110阅读模式
英文:

reading file contents of container bound script

问题

  1. 我试图执行一个作为菜单插件的一部分的模板(通过SpreadsheetApp.getUi().createMenu),并将任意文件内容附加为脚本或样式
  2. ```javascript
  3. function requireGs(scripts) {
  4. console.log(scripts)
  5. return `<script>\n${scripts
  6. .map(function (d) {
  7. const text = ScriptApp.getResource(d).getDataAsString()
  8. return text
  9. })
  10. .join('\n\n')}</script>\n`
  11. }
  12. function requireCSS(scripts) {
  13. return `<style>\n${scripts
  14. .map(function (d) {
  15. return ScriptApp.getResource(d).getDataAsString()
  16. })
  17. .join('\n\n')}</style>\n`
  18. }

它将被用于:

  1. <?!= requireGs(['server/models.gs']); ?>
  2. <?!= requireCSS(['client/stylesheet.html']); ?>

然而,ScriptApp.getResource 不存在,我在文档中找不到它,也找不到替代品。

  1. 2023517日,下午6:56:24 调试 [ 'server/models.gs' ]
  2. 2023517日,下午6:56:24 错误 异常:未找到

如何访问推送到容器绑定脚本项目的文件内容?

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;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 &lt;script&gt;\n${scripts
.map(function (d) {
const text = ScriptApp.getResource(d).getDataAsString()
return text
})
.join(&#39;\n\n&#39;)}&lt;/script&gt;\n

}

function requireCSS(scripts) {
return &lt;style&gt;\n${scripts
.map(function (d) {
return ScriptApp.getResource(d).getDataAsString()
})
.join(&#39;\n\n&#39;)}&lt;/style&gt;\n

}

  1. which would be used as

<?!= requireGs(['server/models.gs']); ?>
<?!= requireCSS(['client/stylesheet.html']); ?>

  1. however, `ScriptApp.getResource` doesn&#39;t exist, and I can&#39;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

  1. How can I access contents of files pushed to a container bound script project?
  2. </details>
  3. # 答案1
  4. **得分**: 3
  5. 显示项目中所有文件和函数
  6. ```javascript
  7. function displayAllScriptFilesAndFunctions() {
  8. var scriptId;
  9. const idresp = SpreadsheetApp.getUi().prompt("ScriptId", "输入脚本 ID", SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
  10. if (idresp.getSelectedButton() == SpreadsheetApp.getUi().Button.OK) {
  11. if (idresp.getResponseText().length == 0) {
  12. scriptId = ScriptApp.getScriptId();
  13. } else {
  14. scriptId = idresp.getResponseText();
  15. }
  16. const url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";
  17. const options = { "method": "get", "muteHttpExceptions": true, "headers": { "Authorization": "Bearer " + ScriptApp.getOAuthToken() } };
  18. const res = UrlFetchApp.fetch(url, options);
  19. let data = JSON.parse(res.getContentText());
  20. let uA = [];
  21. let dA = [];
  22. const files = data.files;
  23. let html = `<strong>文件和函数</strong><br />Id:${scriptId}<hr>`;
  24. files.forEach(file => {
  25. html += Utilities.formatString('<br /><strong>文件名:</strong> %s<br /><strong>类型:</strong> %s', file.name, file.type);
  26. if (file.functionSet && file.functionSet.values && file.functionSet.values.length > 0) {
  27. html += Utilities.formatString('<br /><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;函数名:</strong>')
  28. file.functionSet.values.forEach((func, i) => {
  29. if (!~uA.indexOf(func.name)) { uA.push(func.name); } else { dA.push(func.name); }
  30. html += Utilities.formatString('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;%s', func.name)
  31. });
  32. } else {
  33. html += '<br /><strong>未列出函数。</strong>';
  34. }
  35. html += '<hr>';
  36. });
  37. if (dA.length > 0) {
  38. html += Utilities.formatString('<br><strong>重复函数</strong><hr><br>%s<hr>', dA.join('<br>'))
  39. }
  40. SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600), '文件和函数');
  41. }
  42. }
英文:

All files and functions in a project

  1. function displayAllScriptFilesAndFunctions() {
  2. var scriptId;
  3. const idresp = SpreadsheetApp.getUi().prompt(&quot;ScriptId&quot;, &quot;Enter Script Id&quot;, SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
  4. if (idresp.getSelectedButton() == SpreadsheetApp.getUi().Button.OK) {
  5. if (idresp.getResponseText().length == 0) {
  6. scriptId = ScriptApp.getScriptId();
  7. } else {
  8. scriptId = idresp.getResponseText();
  9. }
  10. const url = &quot;https://script.googleapis.com/v1/projects/&quot; + scriptId + &quot;/content&quot;;
  11. const options = { &quot;method&quot;: &quot;get&quot;, &quot;muteHttpExceptions&quot;: true, &quot;headers&quot;: { &quot;Authorization&quot;: &quot;Bearer &quot; + ScriptApp.getOAuthToken() } };
  12. const res = UrlFetchApp.fetch(url, options);
  13. let data = JSON.parse(res.getContentText());
  14. //SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(&#39;&lt;textarea rows=&quot;50&quot; cols=&quot;120&quot;/&gt;&#39; + res + &#39;&lt;/textarea&gt;&#39;).setWidth(1000).setHeight(500), &#39;Return&#39;);
  15. let uA = [];
  16. let dA = [];
  17. const files = data.files;
  18. let html = `&lt;strong&gt;Files and Functions&lt;/strong&gt;&lt;br /&gt;Id:${scriptId}&lt;hr&gt;`;
  19. files.forEach(file =&gt; {
  20. html += Utilities.formatString(&#39;&lt;br /&gt;&lt;strong&gt;FileName:&lt;/strong&gt; %s&lt;br /&gt;&lt;strong&gt;Type:&lt;/strong&gt; %s&#39;, file.name, file.type);
  21. if (file.functionSet &amp;&amp; file.functionSet.values &amp;&amp; file.functionSet.values.length &gt; 0) {
  22. html += Utilities.formatString(&#39;&lt;br /&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Function Names:&lt;/strong&gt;&#39;)
  23. file.functionSet.values.forEach((func, i) =&gt; {
  24. if (!~uA.indexOf(func.name)) { uA.push(func.name); } else { dA.push(func.name); }
  25. html += Utilities.formatString(&#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%s&#39;, func.name)
  26. });
  27. } else {
  28. html += &#39;&lt;br /&gt;&lt;strong&gt;No functions listed.&lt;/strong&gt;&#39;;
  29. }
  30. html += &#39;&lt;hr&gt;&#39;;
  31. });
  32. if (dA.length &gt; 0) {
  33. html += Utilities.formatString(&#39;&lt;br&gt;&lt;strong&gt;Duplicate Functions&lt;/strong&gt;&lt;hr&gt;&lt;br&gt;%s&lt;hr&gt;&#39;, dA.join(&#39;&lt;br&gt;&#39;))
  34. }
  35. SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600), &#39;Files and Functions&#39;);
  36. }
  37. }

答案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.

huangapple
  • 本文由 发表于 2023年5月18日 01:06:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定