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

问题描述 投票:0回答:2

我正在尝试执行一个模板,该模板是菜单插件的一部分(通过 SpreadsheetApp.getUi().createMenu),其中任意文件内容附加为脚本或样式

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
不存在,我在文档中看不到它,也找不到它的替代品。

May 17, 2023, 6:56:24 PM    Debug   [ 'server/models.gs' ]
May 17, 2023, 6:56:24 PM    Error   Exception: Not found

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

google-apps-script google-sheets google-apps
2个回答
1
投票

一个项目中的所有文件和函数

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');
  }
}

0
投票

似乎必须删除文件扩展名才能正常工作。 ScriptApp.getResource 没有记录,但它现在有效。

© www.soinside.com 2019 - 2024. All rights reserved.