Google幻灯片API-如何更改特定颜色的所有形状的文本颜色 摘要说明参考

问题描述

我有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/

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

感谢有关这项工作的所有提示

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

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

解决方法

如果要将所有文本从#E04935更改为#000000,可以尝试以下代码段:

摘要

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

   }
}

说明

您的代码无法正常运行的原因是,您试图在getForegroundColor()类型的对象上使用TextRange方法,而该方法却有望从{{1 }}。

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

参考

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...