【问题标题】:Google Slides API- How to change text color for all shapes of a certain colorGoogle Slides API-如何更改某种颜色的所有形状的文本颜色
【发布时间】:2020-10-07 21:53:40
【问题描述】:

我有 11 个文件,每个文件有 140 多张幻灯片,并且没有一个形状与主题/母版相关联。我的目标是更改主字体并将所有红色文本(有很多红色文本)替换为黑色文本。

我成功地更新了主字体(感谢https://gist.github.com/dsottimano/20a50daded2128b4c86acb430cecba67),但在尝试为 ForegoundColor 写一些东西时遇到了问题。

我尝试修改此代码但无法使其工作:https://mashe.hawksey.info/2017/10/changing-the-color-of-all-links-in-google-slides-with-google-apps-script/

我需要用十六进制 #000000 替换所有使用前景色十六进制 #e04935 格式化的文本。

感谢有关使这项工作的任何提示!

这是我到目前为止所做的:

//original from: https://mashe.hawksey.info/2017/10/changing-the-color-of-all-links-in-google-slides-with-google-apps-script/
/**
 * @OnlyCurrentDoc
 */
 
function changeTextColorforShapes(){
  var deck = SlidesApp.getActivePresentation();
  var slides = deck.getSlides();
  slides.forEach(function(slide) {
    // https://developers.google.com/apps-script/reference/slides/slide#getPageElements()
    var elements = slide.getPageElements();
    elements.forEach(function(element){
      // https://developers.google.com/apps-script/reference/slides/page-element#getPageElementType()
      var type = element.getPageElementType();
      // Text boxes are 'SHAPES' (this code doesn't handle table cells)
      if (type == "SHAPE"){
        // https://developers.google.com/apps-script/reference/slides/text-range#getTextStyle()
        var textStyle = element.asShape().getText().getForegroundColor();
        // https://developers.google.com/apps-script/reference/slides/text-style#hasLink()
        // Returns true if text is color #e04935 (https://www.color-hex.com/color/e04935) and changes text to color #ffffff (black)
        if (ForegroundColor('#e04935')
          textStyle.setForegroundColor('#ffffff');
      }
    });
  });
}

【问题讨论】:

  • 能否提供but have come up short in trying to write something for ForegoundColor.I tried to adapt this code and cannot make it work:的详细信息?我无法理解I need to replace all text formatted with foregroundcolor hex #e04935 with hex #000000.。可以问一下具体情况吗?
  • 我的幻灯片全都是红色文本(全部为十六进制 #e04935),我需要将所有这些文本更改为黑色。我想我可以使用这个代码的框架,因为它改变了所有带有链接的文本的颜色。我试过了,但我不知道我在做什么: var textStyle = element.asShape().getText().getForegroundColor(); if ForegroundColor('#e04935'){ textStyle.setForegroundColor('#000000');
  • 感谢您的回复。 var textStyle = element.asShape().getText().getForegroundColor(); if ForegroundColor('#e04935'){ textStyle.setForegroundColor('#000000'); 是什么?你能展示你当前的脚本吗?如果可以,请将其添加到您的问题中?
  • 我在我的问题中添加了到目前为止的内容。显然我不知道我在做什么
  • 感谢您的回复。我注意到已经发布了答案。我想尊重现有的答案。我认为它会解决您的问题。

标签: google-api google-slides-api google-slides


【解决方案1】:

如果你想把#E04935的所有文字都改成#000000,可以试试下面的sn-p:

片段

// Copyright 2020 Google LLC.
// SPDX-License-Identifier: Apache-2.0

function changeColor() {
   var presentation = SlidesApp.getActivePresentation();
   var slides = presentation.getSlides();
   for (let i = 0; i < slides.length; i++) {
      var elements = slides[i].getPageElements();
      for (let j = 0; j < elements.length; j++)
         if (elements[j].asShape().getText().getTextStyle().getForegroundColor().asRgbColor().asHexString() == '#E04935')
            elements[j].asShape().getText().getTextStyle().setForegroundColor('#000000');

   }
}

说明

您的代码无法正常工作的原因是您试图对 TextRange 类型的对象使用 getForegroundColor() 方法,而该方法预计会从 TextStyle 类型的对象中调用。

因此,为了准确测试文本的颜色是否为所需颜色,您必须首先将颜色检索为 RGB 颜色,以获取其 HEX 值。

参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    相关资源
    最近更新 更多