You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Google Apps Script插件:CardService设置HTML时CSS/JS显示为文本的问题

Fixing CSS/JS Not Rendering in Google Apps Script CardService Cards

Hey there, let's sort out why your CSS and JavaScript are showing up as plain text instead of working in your CardService card.

The Problem with Your Current Approach

You're using HtmlService.createHtmlOutputFromFile('Index').getContent() to grab your HTML content, but here's the catch: HtmlService is designed for Web Apps, not for CardService's card environment. When you pull the raw HTML string with .getContent(), CardService doesn't parse the embedded CSS or JavaScript—it just treats everything as plain text.

The Correct Solution

Instead of using HtmlService, you need to use CardService's own HtmlOutput class to load and embed your HTML file. This class is built specifically to handle HTML content in cards, including parsing styles and scripts properly.

Here's how to adjust your code:

function buildPage() {
  // Use CardService's HtmlOutput to load your Index.html file directly
  var htmlWidget = CardService.newHtmlOutput().setContentFromFile('Index');
  
  // Build your card as usual
  var card = CardService.newCardBuilder()
    .setHeader(CardService.newCardHeader().setTitle('How do you feel ...'));
  
  // Add the HTML widget to your card section
  var section = CardService.newCardSection()
    .addWidget(htmlWidget);
  
  // Don't forget to build and return the completed card!
  card.addSection(section);
  return card.build();
}

Key Notes to Keep in Mind

  • CardService HTML Limits: Cards have some restrictions on HTML/CSS/JS. Stick to inline styles or <style> tags (avoid external CSS files), and keep your JavaScript focused on client-side interactions that work within the card environment—some global APIs might not be available here.
  • Validate Your Index.html: Make sure your HTML file has proper syntax—wrap styles in <style> tags and scripts in <script> tags, with no typos that could break rendering.

内容的提问来源于stack exchange,提问作者Jai Prak

火山引擎 最新活动