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

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

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&#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

}



which would be used as 

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


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


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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;函数名:</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;&nbsp;&nbsp;%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(&quot;ScriptId&quot;, &quot;Enter Script Id&quot;, 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 = &quot;https://script.googleapis.com/v1/projects/&quot; + scriptId + &quot;/content&quot;;
    const options = { &quot;method&quot;: &quot;get&quot;, &quot;muteHttpExceptions&quot;: true, &quot;headers&quot;: { &quot;Authorization&quot;: &quot;Bearer &quot; + ScriptApp.getOAuthToken() } };
    const res = UrlFetchApp.fetch(url, options);
    let data = JSON.parse(res.getContentText());
    //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;);
    let uA = [];
    let dA = [];
    const files = data.files;
    let html = `&lt;strong&gt;Files and Functions&lt;/strong&gt;&lt;br /&gt;Id:${scriptId}&lt;hr&gt;`;
    files.forEach(file =&gt; {
      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);
      if (file.functionSet &amp;&amp; file.functionSet.values &amp;&amp; file.functionSet.values.length &gt; 0) {
        html += Utilities.formatString(&#39;&lt;br /&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Function Names:&lt;/strong&gt;&#39;)
        file.functionSet.values.forEach((func, i) =&gt; {
          if (!~uA.indexOf(func.name)) { uA.push(func.name); } else { dA.push(func.name); }
          html += Utilities.formatString(&#39;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;%s&#39;, func.name)
        });
      } else {
        html += &#39;&lt;br /&gt;&lt;strong&gt;No functions listed.&lt;/strong&gt;&#39;;
      }
      html += &#39;&lt;hr&gt;&#39;;
    });
    if (dA.length &gt; 0) {
      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;))
    }
    SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html).setWidth(800).setHeight(600), &#39;Files and Functions&#39;);
  }
}

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

确定