Home > Software design >  I would like to know how to build a google script to copy cells A2:K12 with its current cell formats
I would like to know how to build a google script to copy cells A2:K12 with its current cell formats

Time:12-28

I am in need of assistance with google sheet app script in accomplishing the task of highlighting & copying cells A2:K12 with its current cell formats from Sheet A to another sheet's (Sheet B) last available row. Can someone help me in getting this task complete in a function? I have googled some scripts, but they are not what I am looking for, the ones that I have found are only copying values and I would like to copy with the cell formats as it has color highlights and all in my sheet.

CodePudding user response:

Copy from Sheet to Sheet in same Spreadsheet

function myfunk() {
  const ss = SpreadsheetApp.getActive();
  const sha = ss.getSheetByName("SheetA");
  const shb = ss.getSheetByName("SheetB");
  sha.getRange("A2:K12").copyTo(shb.getRange(shb.getLastRow()   1, 1))
}

copyTo

CodePudding user response:

If you simply use "copyTo" method on a range you would like to copy, it will copy everything including the formatting. You would need to be specific if you like to copy just values. It is similar to just copy/pasting without the script. Here is a sample code I wrote to copy data with colored backgrounds in B2:B5 in sheet 1 and copies it to sheet 2 as is.

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sheet1 = ss.getSheetByName('Sheet1');
  const sheet2 = ss.getSheetByName('Sheet2');
  const copyFromRange = sheet1.getRange("B2:B5");
  const copyToRange = sheet2.getRange("B2:B5");
  copyFromRange.copyTo(copyToRange)
}

You should look in 'copyTo' Explaination which has other attributes that you can specify under 'copypastetype'. Hope it helps!

  • Related