[英]Google Sheets: Copying a cell with multiple font styles (colors) to another cell maintaining style
How can I copy a cell with different font colors to another cell?如何将具有不同字体 colors 的单元格复制到另一个单元格? Basically want to perform a standard copy-paste maintaining styles of the copied cell... below are examples of source cells:
基本上想要执行一个标准的复制粘贴来维护复制单元格的 styles ......下面是源单元格的示例:
The format is important as it gives more info based on the different color codes.格式很重要,因为它会根据不同的颜色代码提供更多信息。
Range.copyTo emulates a normal copy paste done manually by a user. Range.copyTo模拟用户手动完成的正常复制粘贴。 It preserves all formatting including rich text.
它保留所有格式,包括富文本。
function copyAsCopiedByUser(){
const sh = SpreadsheetApp.getActive().getSheetByName("Sheet1")
sh.getRange("A1").copyTo(sh.getRange("B2"))
}
Have just tried something and it has worked.刚刚尝试了一些东西,它已经奏效了。 Much easier than I thought in the end.
最后比我想象的要容易得多。
function copyWithFormat() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var sourceRange = sheet.getRange("B1");
var targetRange = sheet.getRange("C1");
var sourceText = SpreadsheetApp.newRichTextValue();
sourceText = sourceRange.getValue().toString();
var sourceFormat = sourceRange.getRichTextValue();
targetRange.setValue(sourceText);
targetRange.setRichTextValue(sourceFormat);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.