css - Google 表格应用程序脚本无法处理 CSS 样式

标签 css user-interface google-apps-script modal-dialog include

我在 Google 电子表格中有一张图片。按下按钮会触发脚本,该脚本应该打开一个风格化的表格。相反,它打开一个表并且无法对其应用 CSS 样式。我阅读了 Google 文档,了解如何制作单独的文件进行样式设置并将包含内容放入主 HTML 文件中,如下所示

//   <?!= include('stylesheet'); ?>

我就是这么做的。我得到一张带有纯文本的表格。 enter image description here

如何让脚本真正处理CSS样式?

gs 文件:

 function include(filename) {
  return HtmlService.createHtmlOutputFromFile(filename).getContent();
};

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Stundu_laiki')
       .setHeight(600);
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .showModalDialog(html, 'Stundu laiki')
      
}

带有表格 CSS 的 stylesheet.html 文件

<style>
.w3-table,.w3-table-all{}.w3-table-all{border:1px solid #ccc}
.w3-table-lined tr:nth-child(odd){background-color:#fff}.w3-table-lined tr:nth-child(even){background-color:#f1f1f1}
.w3-table-all tr:nth-child(odd){background-color:#fff}.w3-table-all tr:nth-child(even){background-color:#f1f1f1}
.w3-bordered tr,.w3-table-all tr{border-bottom:1px solid #ddd}.w3-striped tbody tr:nth-child(even){background-color:#f1f1f1}
</style>

stundu_laiki.html 与表格

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    **<?!= include('stylesheet'); ?>**
  </head>
  <body>
    <table width="98%" border="1" cellpadding="3px" cellspacing="0" style="w3-table-all">
            <tr>
              <td width="23%">1.</td>
              <td width="77%">8:30 - 9:10</td>
            </tr>
            <tr>
              <td>2.</td>
              <td>9:20 - 10:00</td>
            </tr>
            <tr>
              <td>3.</td>
              <td>10:10 - 10:50</td>
            </tr>
            <tr>
              <td>4.</td>
              <td>11:05 - 11:45</td>
            </tr>
            <tr>
              <td>5.</td>
              <td>12:00 - 12:40</td>
            </tr>
            <tr>
              <td>6.</td>
              <td>13:10 - 13:50</td>
            </tr>
            <tr>
              <td>7.</td>
              <td> 14:00 - 14:40</td>
            </tr>
            <tr>
              <td>8.</td>
              <td>14:50 - 15:30</td>
            </tr>
            <tr>
              <td>9.</td>
              <td>15:35 - 16:15</td>
            </tr>
          </table>

<h2> Saīsināto dienu stundu laiki</h2>

<table width="98%" border="1" cellpadding="3px" cellspacing="0" class="w3-table-all">
    <tr>
      <td width="23%">1.</td>
      <td width="77%">8:30 - 9:00</td>
    </tr>
    <tr>
      <td>2.</td>
      <td>9:10 - 9:40</td>
    </tr>
    <tr>
      <td>3.</td>
      <td>9:50 - 10:20</td>
    </tr>
    <tr>
      <td>4.</td>
      <td>10:35 - 11:05</td>
    </tr>
    <tr>
      <td>5.</td>
      <td>11:20 - 11:50</td>
    </tr>
    <tr>
      <td>6.</td>
      <td>12:20 - 12:50</td>
    </tr>
    <tr>
      <td>7.</td>
      <td> 13:00 - 13:30</td>
    </tr>
    <tr>
      <td>8.</td>
      <td>13:40 - 14:10</td>
    </tr>
    <tr>
      <td>9.</td>
      <td>14:15 - 14:45</td>
    </tr>
  </table> 
  </body>
</html>

最佳答案

使用createTemplateFromFile,然后评估您的html。请参阅下面的最终脚本。

最终脚本:

function openDialog() {
  var html = HtmlService.createTemplateFromFile('stundu_laiki')
    .evaluate()
    .setHeight(600);
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
    .showModalDialog(html, 'Stundu laiki')    
}

输出:

enter image description here

关于css - Google 表格应用程序脚本无法处理 CSS 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69135008/

相关文章:

javascript - 我怎样才能将以前的功能添加到这个JS

javascript - Bootstrap 4 导航下拉悬停在桌面上/点击移动设备

javascript - 模态背后的模糊 HTML 内容

java - 如何防止 JFrame 窗口倍增?

Java更改按钮名称并递增计数

javascript - 如何在 Google Apps 插件脚本中使用外部 javascript 库?

javascript - jQuery 在 div 鼠标悬停时更改输入值

c++ - 在 C++ 的 UI 线程中需要帮助

google-apps-script - 谷歌小工具/电子表格 : Grab spreadsheet key for gadget

javascript - 从 Google 应用程序脚本中的 UrlFetchApp.Fetch() 获取响应并在 Logger 上打印