0

I'm trying to programmatically copy everything (data, cells borders, format (%), color) from one set of columns to another.

function myFunction(){
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = sheet.getSheets()[1];
    const range = sheet.getRange('A1:C5').getValues();
    const newrange = sheet.getRange('D1:F5');

    sheet.insertColumnsAfter(3,3);    
    newrange.setValues(range);
    [...]

Here the data as well as the color are copied correctly to the new location. However, I could not figure out how to replicate:

  • The cell border lines (to draw a table)
  • The format of my cells in percentage (including the sign %).

I've tried the following for the percentage - but did not worked:

const percentageRange = sheet.getRange('A1:C5');
percentageRange.setNumberFormat('00.00"%"');

Not sure how should I do for the border

1 Answers1

0

Try this:

range.copyTo(newrange); 

this will copy range completely.

Reference: https://stackoverflow.com/a/44967382/555121

Kos
  • 4,890
  • 9
  • 38
  • 42
  • Thank you @Kos it works fine, except for the cells format. 2 of my columns are in % but it does not appear in % when I copy. For instance 17.00% becomes 00.17% in the newly created column – AnotherEukaryote Aug 16 '22 at 11:38
  • Let me know if you have any clue how to solve the % format not being displayed properly. I would really appreciate :) – AnotherEukaryote Aug 19 '22 at 10:48
  • just for the record: copyTo copies everything, including format. If you have any other questions, refer to official documentation – Kos Aug 19 '22 at 10:57