Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and paste Value and Format Only - Google Script

I'd like to copy and paste just values and format - not formula..

E.G (A1 is a dynamic text):

A1 = "test"

A2 = =A1

I'd like to use a script that when activated it copy A2 format and text (in this case would copy "test" - not "=a1") to A3 .

I tried with "formatOnly:true , contentsOnly:true" - but it keep copying formula.

function Threebrow() {  
   var sheet2 = SpreadsheetApp.getActive().getSheetByName('c');
  sheet2.getRange("a2").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1,1,7), {formatOnly:true , contentsOnly:true});
}
like image 229
K. Bert Avatar asked Sep 27 '17 20:09

K. Bert


1 Answers

Formulas are also counted as content in this context. The below function copies value and format of A2 to the lowest row in the first column.

function Threebrow() {  
  var sheet2 = SpreadsheetApp.getActive().getSheetByName('c');
  var sourceRange = sheet2.getRange("a2");
  var targetRange = sheet2.getRange(sheet2.getLastRow()+1,1,1,1);
  targetRange.setValues(sourceRange.getValues());
  sourceRange.copyTo(targetRange, {formatOnly:true})
}
like image 180
Robin Gertenbach Avatar answered Sep 19 '22 13:09

Robin Gertenbach