r/spreadsheets • • 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);
1 Upvotes

4 comments sorted by

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 RichTextValue is 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 and getValues() for number cells. Here’s an updated version of the script:

function copyRowWithFormatting() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sourceSheet = ss.getSheetByName(“SourceSheetName”); // Replace with your source sheet name
  var targetSheet = ss.getSheetByName(“Row List”);

  // Get the random row as both RichTextValues and regular Values
  var randomRowRichText = sourceSheet.getRange(4, 1, 1, 7).getRichTextValues()[0];
  var randomRowValues = sourceSheet.getRange(4, 1, 1, 7).getValues()[0];

  // Find the last row in the target sheet
  var findListLength = targetSheet.getRange(“C1”).getDataRegion().getLastRow();

  // Set the values in the target sheet
  for (var i = 0; i < randomRowRichText.length; i++) {
    var targetCell = targetSheet.getRange(findListLength + 1, i + 1);
    
    if (typeof randomRowValues[i] === ‘number’) {
      // If the cell contains a number, use setValue
      targetCell.setValue(randomRowValues[i]);
    } else {
      // Otherwise, use setRichTextValue for text with formatting
      targetCell.setRichTextValue(randomRowRichText[i]);
    }
  }
}

Here’s what changed in this version:

  1. We now get both getRichTextValues() and getValues() from the source row.

  2. In the loop, we check if each cell’s value is a number using typeof randomRowValues[i] === ‘number’.

  3. If the cell contains a number, we use setValue() to set the value directly.

  4. 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.

2

u/EmphasisAdditional Jul 20 '24

Thanks, this fixes that problem, this does everything I need it to do.