Home > Back-end >  Copying a specific range with bolded segment into a specific range on another spreadsheet while pres
Copying a specific range with bolded segment into a specific range on another spreadsheet while pres

Time:10-28

What I have

On a google spreadsheet, I have a range (A1:B1), like this:

spreadsheetA | sheet1

A B
1 Hello world, this text is bolded, while the rest is not Some text

What I want

I want to copy the text into a range (D3:E3) in a separate google spreadsheet, so it will have the same text and the same bolded section(s), like this:

spreadsheetB | sheet2

D E
3 Hello world, this text is bolded, while the rest is not Some text

What I have now

I am currently using .setValues() which results in copying only the plain text (as .copyTo() can only be used in the same spreadsheet), like this:

spreadsheetB | sheet2

D E
3 Hello world, this text is bolded, while the rest is not Some text

The (simplified) code for reference:

function syncToSpreadsheetB(sheet1) { // found under spreadSheetA
  var spreadSheetB = SpreadsheetApp.openById('spreadSheetB_id');
  var sheet2 = spreadSheetB.getSheetByName('sheet2');

  if (sheet2 === null) return;

  sheet2.getRange(3,4,1,2).setValues(sheet1.getRange(1,1,1,2).getValues());
}

I know that there is the RichTextValue class, but wasn't sure how to use it for what I need.

CodePudding user response:

In your situation, how about using RichTextValue? This has also already mentioned in your question. So, how about the following sample script?

Sample script:

function myFunction() {
  var spreadsheetA = SpreadsheetApp.openById('###');
  var sheet1 = spreadsheetA.getSheetByName("Sheet1");
  var srcRange = sheet1.getRange(1,1,1,2); // This range is from your script.
  var srcRichTextValues = srcRange.getRichTextValues();

  var spreadSheetB = SpreadsheetApp.openById('spreadSheetB_id');
  var sheet2 = spreadSheetB.getSheetByName('sheet2');
  sheet2.getRange(3,4,1,2).setRichTextValues(srcRichTextValues); // This range is from your script.
}

References:

  • Related