如何根据从Google表格中选择的范围在Google文档中更改文本颜色。

huangapple go评论70阅读模式
英文:

How to change the text color in google-docs based on an range selected from google-sheets

问题

我有一个包含不同颜色的各种单元格的Google表格,我能够创建一个脚本,用表格范围中的值替换Google文档模板,但我无法在替换值时保留它们的原始颜色。
我能够捕获单元格的颜色,但找不到应用它们的方法。

setForegroundColor(color); 只能将文本颜色更改为黑色...

我目前的代码如下:

  const template = DriveApp.getFileById('sadfasfadjkg');
  const destination = DriveApp.getFolderById('sadfasfadjkg');
  const range = SpreadsheetApp.getActiveSheet().getRange('C3:E26');
  const rangeValues = range.getDisplayValues();
  const richTextValues = range.getRichTextValues();
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Data');
  const today = `${new Date().getDate()}/${new Date().getMonth()+1}/${new Date().getFullYear()}`;
  const copyDoc = template.makeCopy(`Report ${today}`, destination);
  const doc = DocumentApp.openById(copyDoc.getId());
  const body = doc.getBody();

  for (let x = 0; x < rangeValues.length; x++) {
    for (let y = 0; y < rangeValues[x].length; y++) {
      const cellTxt = richTextValues[x][y];
      const style = cellTxt.getTextStyle();
      const color = style.getForegroundColor();
      
      body.replaceText(`{{${x}-${y}}}`, rangeValues[x][y]).setForegroundColor(color);
    }
  }
  doc.saveAndClose();
  const url = doc.getUrl();
  sheet.getRange('J1').setValue(url);
}
英文:

I have a google sheets with various cells containing diferent colors, i was able to make a script that replaces a google-docs template with the values from the sheets range, but im not being able to also change the color with it's original when i replace the values.
I was able to capture the colors from the cells but i can't find a way to apply it.

the setForegroundColor(color); changes the text color to black only...

The code i have till now:

  const template = DriveApp.getFileById(&#39;sadfasfadjkg&#39;);
  const destination = DriveApp.getFolderById(&#39;sadfasfadjkg&#39;);
  const range = SpreadsheetApp.getActiveSheet().getRange(&#39;C3:E26&#39;);
  const rangeValues = range.getDisplayValues();
  const richTextValues = range.getRichTextValues();
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(&#39;Data&#39;);
  const today = `${new Date().getDate()}/${new Date().getMonth()+1}/${new Date().getFullYear()}`;
  const copyDoc = template.makeCopy(`Report ${today}`, destination);
  const doc = DocumentApp.openById(copyDoc.getId());
  const body = doc.getBody();

  for (let x = 0; x &lt; rangeValues.length; x++) {
    for (let y = 0; y &lt; rangeValues[x].length; y++) {
      const cellTxt = richTextValues[x][y];
      const style = cellTxt.getTextStyle();
      const color = style.getForegroundColor();
      
      body.replaceText(`{{${x}-${y}}}`, rangeValues[x][y]).setForegroundColor(color);
    }
  }
  doc.saveAndClose();
  const url = doc.getUrl();
  sheet.getRange(&#39;J1&#39;).setValue(url);
}

</details>


# 答案1
**得分**: 2

我猜您期望的结果如下所示。

- 您想要用电子表格的单元格值替换Google文档中的占位符,并且还想复制单元格值的字体颜色。

尽管不幸的是,我不确定我是否正确理解了您的脚本中的`但我不能改变颜色与它的原始颜色`,不过以下修改如何?

### 从:
```javascript
const richTextValues = range.getRichTextValues();

到:

const fontColor = range.getFontColorObjects();

并且,请按照以下方式进行修改。

从:

const cellTxt = richTextValues[x][y];
const style = cellTxt.getTextStyle();
const color = style.getForegroundColor();

body.replaceText(`{{${x}-${y}}}`, rangeValues[x][y]).setForegroundColor(color);

到:

const forgroundColor = fontColor[x][y].asRgbColor().asHexString().slice(0, 7);
const search = `{{${x}-${y}}}`;
let s = body.findText(search);
while (s) {
  const e = s.getElement();
  const start = s.getStartOffset();
  const text = e.asText().replaceText(search, rangeValues[x][y]);
  text.asText().setForegroundColor(start, start + rangeValues[x][y].length, forgroundColor);
  s = e.findText(search, s);
}
  • 当运行这个修改后的脚本时,每个占位符都会被替换为单元格值,并且单元格值的字体颜色也会被反映出来。

参考:

英文:

I guessed your expected result is as follows.

  • You want to replace the placeholders in Google Documents with the cell values of Spreadsheet, and also, you want to copy the font color of the cell value.

Although, unfortunately, I'm not sure whether I could correctly understand but im not being able to also change the color with it&#39;s original when i replace the values., in your script, how about the following modification?

From:

const richTextValues = range.getRichTextValues();

To:

const fontColor = range.getFontColorObjects();

And, please modify as follows.

From:

const cellTxt = richTextValues[x][y];
const style = cellTxt.getTextStyle();
const color = style.getForegroundColor();

body.replaceText(`{{${x}-${y}}}`, rangeValues[x][y]).setForegroundColor(color);

To:

const forgroundColor = fontColor[x][y].asRgbColor().asHexString().slice(0, 7);
const search = `{{${x}-${y}}}`;
let s = body.findText(search);
while (s) {
  const e = s.getElement();
  const start = s.getStartOffset();
  const text = e.asText().replaceText(search, rangeValues[x][y]);
  text.asText().setForegroundColor(start, start + rangeValues[x][y].length, forgroundColor);
  s = e.findText(search, s);
}
  • When this modified script is run, each placeholder is replaced with the cell value, and the font color of the cell value is also reflected.

References:

huangapple
  • 本文由 发表于 2023年6月13日 02:31:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459382.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定