I'm just getting my way around Google Sheets script, and I'm finding it a little difficult to zero in on the problem. The code apparently copies empty values from column 4 to column 3, when I use the Range.setFormulaR1C1
in the line just above the Range.copyTo
statement.
function cleanUpNames() {
var currentSheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var currentSheetLastRow = currentSheet.getLastRow();
//Range of cells in the third column, that are just names
var firstNames = currentSheet.getRange('C2:C' + currentSheetLastRow);
//Range of cells in the fourth column
var newFirstNames = currentSheet.getRange('D2:D' + currentSheetLastRow);
//Using the fourth column to replace period character(.) with empty string
var formulaString = '=SUBSTITUTE(R[0]C[-1],".","")';
newFirstNames.setFormulaR1C1(formulaString);
//This line is giving empty values on the third column, instead of pasting the actual formula results
newFirstNames.copyTo(firstNames, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
However, when I do away with the code that sets formulas and enter the corresponding formula from the UI, then run the following code, it works just fine. I'm really not sure where I'm getting this wrong. Any help would be very much appreciated.
function cleanUpNames() {
var currentSheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var currentSheetLastRow = currentSheet.getLastRow();
//Range of cells in the third column, that are just names
var firstNames = currentSheet.getRange('C2:C' + currentSheetLastRow);
//Range of cells in the fourth column
var newFirstNames = currentSheet.getRange('D2:D' + currentSheetLastRow);
//This line is giving empty values on the third column, instead of pasting the actual formula results
newFirstNames.copyTo(firstNames, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};