Google-sheets – Copy one range to another column, then clear the original range

google sheetsgoogle-apps-script

I have a Google Sheets that is updated daily with new inventory data. Currently I have to insert a column, copy and paste the data from A15-50 into C15-50.

Is there any way to insert a column where column C is (shifting data to the right), copy A15-A50 to C15-50 so that yesterday's data goes into a different column (C), and (A) gets cleared out so new data can be input for the day?

Currently I have to have all data cleared from column C or it will paste the data after any data in the column. ie: C15-50 is clear, but C51 has data so the data posts in C52. I am currently copying one cell at a time using:

function copypasta() {
var sourceSheet = "Sheet1";
var sourceCell = "B15";
var destinationSheet = "Sheet1";
var destinationCell = "C15"; 
var ss = SpreadsheetApp.getActive();
var sourceValue = ss.getSheetByName(sourceSheet).getRange(sourceCell).getValue();
var destSheet = ss.getSheetByName(destinationSheet);

var destRange = destSheet.getRange(destinationCell);

var destRow = destRange.getRow();

var destCol = destRange.getColumn();

var destValues = destSheet.getRange(destRow,destCol,Math.max(1,destSheet.getLastRow()-destRow+1)).getValues();

 for (var i=destValues.length-1;i>=0;--i) {    if (destValues[i]) break;   }
 if (destValues[i]!="") i=i+1;   destSheet.getRange(destRow+i,destCol).setValue(sourceValue);   }

then:

 function clearOrder() {
  SpreadsheetApp.getActiveSheet().getRange('C15').setValue('');
}

Could I change the script to overwrite the data that is in column C?
Is there any way to work on the whole range (C15-50) as opposed to doing it one cell at a time?

I have a little scripting experience but I would consider myself an amateur.

Best Answer

var sourceRange = sourceSheet.getRange("A15:A50");
var destRange=sourceSheet.getRange("C15:C50"); 
sourceRange.copyTo(destRange);
sourceRange.clearContent();