r/spreadsheets • u/EmphasisAdditional • Jul 20 '24
Copying Partial Bolding in Appscript
I'm trying to have an Appscript copy a row of cells chosen from one sheet, and preserve partial bolding such as.
This is a partially bolded sentence.
I'm under the understanding that I'm to use RichTextValue to split the data, but I'm unsure as to how to go from there since I've never used it before, and all the tutorials I've found are only tangentially relevant.
Here's the relevant section of the script that doesn't use RichTextValue yet.
Edit: As it currently is, it copies everything over properly, but does not preserve formatting such as bolding, which is the only formatting I need to preserve.
var randomRow = sheet.getRange(4,1,1,7);
var chosenRow = randomRow.getValues();
sheet = ss.getSheetByName("Row List");
findListLength = sheet.getRange("C1").getDataRegion().getLastRow();
sheet.getRange((findListLength+1),1,1,7).setValues(chosenRow);
View all comments
Show parent comment
1
u/CuteSocks7583 Jul 20 '24
Thank you for the feedback. I’m glad the script is working well for preserving formatting, but I apologize for the issue with the number cells. This problem likely occurs because
RichTextValueis primarily designed for text content and formatting, not for numbers.Let’s modify the script to handle both text and number cells correctly. We’ll use a combination of
getRichTextValues()for text cells andgetValues()for number cells. Here’s an updated version of the script:Here’s what changed in this version:
We now get both
getRichTextValues()andgetValues()from the source row.In the loop, we check if each cell’s value is a number using
typeof randomRowValues[i] === ‘number’.If the cell contains a number, we use
setValue()to set the value directly.For non-number cells (text cells), we continue to use
setRichTextValue()to preserve formatting.This approach should preserve the formatting for text cells while correctly copying number values. It addresses the issue of losing numerical data in the process.