One of my longstanding techniques with spreadsheets is Copy / Paste Special Values (C/PSV), in place. Having used formulas to produce the values I'm interested in, I C/PSV and can then delete the source data.
So I wrote a macro which uses this technique, but the cells wind up empty. But if I split the macro into two, ending the first macro before C/PSV, then everything works as intended. Why is this? Is there a better way to work around this problem? Here are my three macros.
function Step1() {
var spreadsheet = SpreadsheetApp.getActive();
var range = spreadsheet.getActiveRange();
CopyRangeToNewSheet(spreadsheet, range);
spreadsheet.getCurrentCell().offset(-1, 6).activate();
FillInHeaders(spreadsheet);
spreadsheet.getCurrentCell().offset(1, -4).activate();
FillInFormulas(spreadsheet);
spreadsheet.getCurrentCell().offset(0, -4, range.getNumRows(), 5).activate();
spreadsheet.getCurrentCell().offset(0, 0, 1, 5).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
};
function Step2() {
var spreadsheet = SpreadsheetApp.getActive();
var keepers = spreadsheet.getRange('G:J');
keepers.activate();
keepers.copyTo(keepers, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
var discard = spreadsheet.getRange('A:F')
discard.activate();
spreadsheet.getActiveSheet().deleteColumns(discard.getColumn(), discard.getNumColumns());
};
function BothSteps() {
var spreadsheet = SpreadsheetApp.getActive();
var range = spreadsheet.getActiveRange();
CopyRangeToNewSheet(spreadsheet, range);
spreadsheet.getCurrentCell().offset(-1, 6).activate();
FillInHeaders(spreadsheet);
spreadsheet.getCurrentCell().offset(1, -4).activate();
FillInFormulas(spreadsheet);
spreadsheet.getCurrentCell().offset(0, -4, range.getNumRows(), 5).activate();
spreadsheet.getCurrentCell().offset(0, 0, 1, 5).copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
var keepers = spreadsheet.getRange('G:J');
keepers.activate();
keepers.copyTo(keepers, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
var discard = spreadsheet.getRange('A:F')
discard.activate();
spreadsheet.getActiveSheet().deleteColumns(discard.getColumn(), discard.getNumColumns());
};
function FillInHeaders(spreadsheet) {
spreadsheet.getCurrentCell().setValue('First Name');
spreadsheet.getCurrentCell().offset(0, 1).activate();
spreadsheet.getCurrentCell().setValue('Last Name');
spreadsheet.getCurrentCell().offset(0, 1).activate();
spreadsheet.getCurrentCell().setValue('Middle Name');
spreadsheet.getCurrentCell().offset(0, 1).activate();
spreadsheet.getCurrentCell().setValue('Email');
}
function FillInFormulas(spreadsheet) {
spreadsheet.getCurrentCell().setFormulaR1C1('=find(" ",R[0]C[-2])');
spreadsheet.getCurrentCell().offset(0, 1).activate();
spreadsheet.getCurrentCell().setFormulaR1C1('=if(iserr(R[0]C[-1]),R[0]C[-3],mid(R[0]C[-3],1,R[0]C[-1]))');
spreadsheet.getCurrentCell().offset(0, 1).activate();
spreadsheet.getCurrentCell().setFormulaR1C1('=if(iserr(R[0]C[-2]),"",mid(R[0]C[-4],R[0]C[-2]+1,50))');
spreadsheet.getCurrentCell().offset(0, 2).activate();
spreadsheet.getCurrentCell().setFormulaR1C1('=R[0]C[-5]');
}
function CopyRangeToNewSheet(spreadsheet, range) {
var newSheet = spreadsheet.insertSheet(1);
spreadsheet.setActiveSheet(newSheet, true);
spreadsheet.getCurrentCell().offset(1, 0).activate();
range.copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
Here is the spreadsheet itself, with tabs Main, Result of Step1, Step2, and Result of Combined Steps: https://docs.google.com/spreadsheets/d/1_nabq_mHuegz_eMIPPAlIgonv71Jh6OPi6qKzeNGGTI/edit?usp=sharing