I have two Google Apps Script functions that should work the same way, but one copies formatting properly while the other doesn’t. Both functions grab calendar data from monthly sheets and put it on a display sheet called “ViewCalendar”. The moveNext function works fine and copies both values and formatting. But the movePrevious function only copies the values and ignores the formatting completely. I can’t figure out why they behave differently when the code looks almost identical. Has anyone run into this before?
function moveNext() {
var workbook = SpreadsheetApp.getActive();
var currentMonth = workbook.getRange('D20').getValue() + 1;
var nextMonth = currentMonth + 1;
workbook.getSheetByName(currentMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('A3:G16'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
workbook.getSheetByName(currentMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('A3:G16'), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
workbook.getSheetByName(nextMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('I3:O16'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
workbook.getSheetByName(nextMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('I3:O16'), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
workbook.getActiveSheet().getRange('D20').setValue(currentMonth);
}
function movePrevious() {
var workbook = SpreadsheetApp.getActive();
var currentMonth = workbook.getRange('D20').getValue() - 1;
var nextMonth = currentMonth + 1;
workbook.getSheetByName(currentMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('A3:G16'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
workbook.getSheetByName(currentMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('A3:G16'), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
workbook.getSheetByName(nextMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('I3:O16'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
workbook.getSheetByName(nextMonth).getRange('A3:G16').copyTo(workbook.getSheetByName('ViewCalendar').getRange('I3:O16'), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
workbook.getActiveSheet().getRange('D20').setValue(currentMonth);
}