I have a spreadsheet with hundreds of lines of data, coming from data validation lists.
Some of the data is highly relevant, therefore I would like it larger, some is useful but superfluous to most.
Different font size is fine. If anyone is an expert with though, different colours and format would be amazing
I would need the script to be dynamic in the sense that data to the left of the | is formatted one way and to the right of the |in another way.
I believe your goal as follows.
google-sheets-macros
).CODE
of NAME NAME | CODE
NAME NAME | CODE
.
NAME NAME
+
+ |
+
. CODE
.Different font size is fine. If anyone is an expert with though, different colours and format would be amazing
Different font size is fine. If anyone is an expert with though, different colours and format would be amazing
, you want to modify the font size and foreground color of the text. In this case, format
you expect is the bold and italic?NAME NAME
of NAME NAME | CODE
NAME NAME | CODE
. I think that above goal can be achieved using RichTextValue. In this answer, I would like to propose the sample script.
This sample script supposes the sample situation that the value of NAME NAME | CODE
NAME NAME | CODE
are put in the column "A". So in order to test it, please prepare this situation.
Please copy and paste the following script to the container-bound script of Spreadsheet, and set the sheet name.
function myFunction() {
const sheetName = "Sheet1"; // Please set the sheet name.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetName);
const range = sheet.getRange("A1:A" + sheet.getLastRow());
const richTextValues = range.getRichTextValues().map(([a]) => {
const text = a.getText();
const pos = text.indexOf("|");
if (pos > -1) {
const temp = a.getTextStyle(0, pos - 1);
const textStyle = SpreadsheetApp.newTextStyle()
.setFontSize(5)
.setForegroundColor("red")
.setItalic(true)
.setBold(true)
.build();
return [
SpreadsheetApp.newRichTextValue()
.setText(text)
.setTextStyle(0, pos - 1, temp)
.setTextStyle(pos + 2, text.length, textStyle)
.build()
];
}
return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
});
range.setRichTextValues(richTextValues);
}
When you run the above script, the text styles of column "A" are reformatted and the text style of CODE
is modified, while the text style of NAME NAME
of NAME NAME | CODE
NAME NAME | CODE
is not modified. And also, the text style of the cell without |
is not modified. You can see the sample result at the following image.
When you want to convert the selected range, how about the following sample script? When you use this script, at first, please select the range and run this script. By this, the selected range is converted.
function myFunction() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const select = ss.getSelection();
const sheet = select.getActiveSheet();
const range = select.getActiveRange();
const richTextValues = range.getRichTextValues().map(([a]) => {
const text = a.getText();
const pos = text.indexOf("|");
if (pos > -1) {
const temp = a.getTextStyle(0, pos - 1);
const textStyle = SpreadsheetApp.newTextStyle()
.setFontSize(5)
.setForegroundColor("red")
.setItalic(true)
.setBold(true)
.build();
return [
SpreadsheetApp.newRichTextValue()
.setText(text)
.setTextStyle(0, pos - 1, temp)
.setTextStyle(pos + 2, text.length, textStyle)
.build()
];
}
return [SpreadsheetApp.newRichTextValue().setText(text).setTextStyle(a.getTextStyle()).build()];
});
range.setRichTextValues(richTextValues);
}
If you want to put the converted values to one right side of the column, please modify above script as follows.
From
range.setRichTextValues(richTextValues);
To
range.offset(0, 1).setRichTextValues(richTextValues);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.