如何使用 Google Web 应用程序在当前页面中附加 html 页面?

发布于 2025-01-18 12:37:18 字数 1171 浏览 3 评论 0原文

我知道如何在 Web 脚本中使用应用程序脚本包含 CSS 或 JS 文件。但这种方式包括页面资源上的文件内容。

我的问题是否可以在当前打开的页面中包含部分html页面?

应用程序脚本包含CSS或JS

/* @Include JavaScript & CSS & HTML-Partial-Views  */
function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

,我这样使用

<?!= include('Css'); ?>

,这是我的尝试。

html

<button onclick="getList("users")">show some html content</button> 
<div id="users"></div>
<script>
function getList(users){
   var listUsers = google.script.run.showHtml(users);
   // how to return showHtml result [list of users]
   for (var i; i <= listUsers.length; 1++)
   {
      document.querySelector("#users").innerHTML += <div>listUsers[i]</div>; 
   }
 }
</script>

GS

function users(sheetName, pageName){
   // get users from sheet
   var ss= SpreadsheetApp.openById("435yh35h45b35nh6hg5bwh455j");
   var dataSheet = ss.getSheetByName(sheetName);
   var dataRange = dataSheet.getDataRange().getValues();
   return dataRange;
}

I know how to include CSS or JS files using app script in a web script. but that way includes files content on page lode.

My question is it possible to include partial html page inside the currently opened page?

app script to include CSS or JS

/* @Include JavaScript & CSS & HTML-Partial-Views  */
function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
}

and I use like this

<?!= include('Css'); ?>

and here is my attempt.

html

<button onclick="getList("users")">show some html content</button> 
<div id="users"></div>
<script>
function getList(users){
   var listUsers = google.script.run.showHtml(users);
   // how to return showHtml result [list of users]
   for (var i; i <= listUsers.length; 1++)
   {
      document.querySelector("#users").innerHTML += <div>listUsers[i]</div>; 
   }
 }
</script>

gs

function users(sheetName, pageName){
   // get users from sheet
   var ss= SpreadsheetApp.openById("435yh35h45b35nh6hg5bwh455j");
   var dataSheet = ss.getSheetByName(sheetName);
   var dataRange = dataSheet.getDataRange().getValues();
   return dataRange;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空宴 2025-01-25 12:37:18

来自代码片段之一

// 如何返回showHtml结果[用户列表]

试试这个:

<button onclick="getList('users')">show some html content</button> 
<div id="users"></div>
<script>
function getList(users){
   var listUsers = google.script.run
     .withSuccessHandler((listUsers) => {
       let list = '';
       for (var i; i < listUsers.length; i++) {
          list += `<div>${listUsers[i]}</div>\n`; 
       }
       document.querySelector("#users").innerHTML = list;
     })
     .showHtml(users);
   
 }
</script>

更改完成

  1. "users" 替换为 'users'
  2. 添加 withSuccessHandler 并使用箭头函数作为回调。
    箭头函数使用 for 语句(因为它在原始代码中使用)构建一个字符串,其中包括使用模板文字的 div 标记。

From one of the code snippets

// how to return showHtml result [list of users]

Try this:

<button onclick="getList('users')">show some html content</button> 
<div id="users"></div>
<script>
function getList(users){
   var listUsers = google.script.run
     .withSuccessHandler((listUsers) => {
       let list = '';
       for (var i; i < listUsers.length; i++) {
          list += `<div>${listUsers[i]}</div>\n`; 
       }
       document.querySelector("#users").innerHTML = list;
     })
     .showHtml(users);
   
 }
</script>

Changes done

  1. Replaced "users" by 'users'
  2. Added withSuccessHandler with an arrow function as callback.
    The arrow function build a string using a for statement (because it was used in the original code) including a div tag using a template literal.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文