I have the code below that works perfectly, but I need to alter it to get data from the same range (D5) but from multiple sheets. At the moment, it gets the data from 'Cancellations' only. How would I get it to select D5 from say 'Sheet2', or 'Sheet3' as well.
I have included half of the code from the function I currently have. As you can see it's very repatative.
function transferList() {
///////////////////////////////////////////Export value 1/////////////////////////////////////////////////////
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 1; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D5").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(2,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(3,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 2/////////////////////////////////////////////////////
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 4; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D6").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 3////////////////////////////////////////////////////////
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 7; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D7").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
///////////////////////////////////////////Export value 4///////////////////////////////////////////////////////////
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Cancellations");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Unanswered");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
var columnIndex = 10; // Column
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Queries");
var sourceData = sourceSheet.getRange("D9").getValue();
var targetSS = ss.getSheetByName("Deduction Breakdown");
var a1Range = targetSS.getRange(5,columnIndex);
var targetRange;
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange(6,columnIndex);
targetRange.setValue(sourceData);
Here is a full script that should do what you are asking:
/** #OnlyCurrentDoc */
function transferList() {
var columnIndex = 1; // Column
var columnIndexIncr = 3; //columnIndex increment per each target range
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheets = ["Cancellations", "Unanswered", "Queries"]; //Sheet names
var targetSS = ss.getSheetByName("Deduction Breakdown"); //Target sheet
var targetRanges = ["D5", "D6", "D7", "D9"]; //Target ranges
var sourceData;
var a1Range;
var a1Row;
var targetRange;
for(var range of targetRanges) {
for(var i in sourceSheets) {
if(range == "D5") {a1Row = 2} else {a1Row = 5};
sourceData = ss.getSheetByName(sourceSheets[i]).getRange(range).getValue();
if(sourceData == "N") {
a1Range = targetSS.getRange(a1Row, columnIndex);
if (a1Range.offset(1,0).getValue() !== "") targetRange = a1Range.getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
else targetRange = targetSS.getRange((a1Row+1),columnIndex);
targetRange.setValue(sourceData);
}
}
columnIndex += columnIndexIncr;
};
};
Please let me know if you have any issues with this. I tested this script in a test sheet and it should work. Please let me know if this was not the intended result
multiple sheets same range
function transferList() {
const ss = SpreadsheetApp.getActive();
const dl = [{srcname:'Cancellations',srcrng:'D5',dstname:'Deduction Breakdown',dstrng:'A2'}];//fill in the remaining options
dl.forEach(o => {
let ssh = ss.getSheetByName(o.srcname);
let srg = ssh.getRange(o.srcrng);
let sda = srg.getValue();
let dsh = ss.getSheetByName(o.dstname);
let drg = dsh.getRange(o.dstrng);
if(drg.offset(1,0).getValue() !== "") {
drg = drg,getNextDataCell(SpreadsheetApp.Direction.DOWN).offset(1,0);
} else {
drg = drg.offset(1,0);
}
drg.setValue(sda);
})
}
Related
I build a range that I am trying to reassign from one sheet to another, but it fails with an error.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var destinationSheet = ss.getSheetByName('Sheet2');
thisWeek = 37;
var firstWeek = [];
var unprotectRange = [];
firstWeek[0] = sourceSheet.getRange('E6:E12');
firstWeek[1] = sourceSheet.getRange('F8:F10');
const columnsPerWeek = 51;
for (var k = thisWeek - 1; k < 53; k++) {
for (var l = 0; l < firstWeek.length; l++) {
if (
sourceSheet
.getRange(firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation())
.isBlank()
) {
unprotectRange.push(
sourceSheet.getRange(
firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation()
)
);
}
}
}
var range2 = destinationSheet.getRange(unprotectRange.getA1Notation());
var protection = destinationSheet.protect().setDescription('Maintain');
protection.setUnprotectedRanges(range2);
}
Now I get the error: 'TypeError: unprotectRange.getA1Notation is not a function' at the statement
var range2 = destinationSheet.getRange(unprotectRange.getA1Notation());
unprotectRange is a array of ranges from source sheet. They need to be strings(A1Notations). Push only the A1 notations.
Convert the strings(A1notations) to ranges of destination sheet using array.map
setProtectedRanges() will then accept this array.
/*...*/
if (
sourceSheet
.getRange(firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation())
.isBlank()
) {
unprotectRange.push(
firstWeek[l].offset(0, columnsPerWeek * k).getA1Notation()/*Modified*/
);
}
/*...*/
const ranges2 = unprotectRange.map(a1Notation => destinationSheet.getRange(a1Notation));
const protection = destinationSheet.protect().setDescription('Maintain');
protection.setUnprotectedRanges(ranges2);
protection.removeEditors(protection.getEditors())
Trying to create a trigger to add a timestamp to a column, if that row was edited at some point that day. Its for work, they feel that onEdit is too messy. I don't know either.
So, I have a trigger set up to run at 1pm every day. That part works. However, I don't know how to make the code for the timestamp to point to that triggered event, and I don't know how to write out that particular function. This is what I've got, it hits line ten and says the source is defined as null. Am I looking for the triggerUiD that it needs, or something else?
function myFunction(event) {
var timeZone = Session.getScriptTimeZone();
var timestamp_format = "MM-dd-yyy hh:mm:ss";
var updateColName = "Updated";
var timeStampColName = "Timestamp";
var source = SpreadsheetApp.getActive().getSheetByName("TestTimestamp");
var actRng = SpreadsheetApp.getActive().getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = SpreadsheetApp.getActive().getSheetByName("TestTimestamp").getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol +1);
var date = Utilities.formateDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
Line 10 sheet is undefined in var headers = SpreadsheetApp.getActive().getSheetByName("TestTimestamp").getRange(1, 1, 1, sheet.getLastColumn()).getValues();
You could rewrite it like this:
function myFunction(event) {
var ss=SpreadsheetApp.getActive();
var timeZone = Session.getScriptTimeZone();
var timestamp_format = "MM-dd-yyy hh:mm:ss";
var updateColName = "Updated";
var timeStampColName = "Timestamp";
var source = ss.getSheetByName("TestTimestamp");
var actRng = ss.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var sheet=ss.getSheetByName('TestTimestamp'');
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
var dateCol = headers.indexOf(timeStampColName);
var updateCol = headers.indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) {
var cell = sheet.getRange(index, dateCol +1);
var date = Utilities.formateDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
I'm using this code to move completed to one sheet but everything else from completed back to current. Is there a way to make it quicker like everything that isn't completed move back? Also is there a way to add sort by column 4 in the same code for the current sheet?
function onEdit() {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs
// see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion
var sheetNameToWatch = "Current";
var columnNumberToWatch = 4;
var valueToWatch = "Completed";
var sheetNameToMoveTheRowTo = "Completed";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "At Risk";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Hold";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Placed";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Safe";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Completed";
var columnNumberToWatch = 4;
var valueToWatch = "Other";
var sheetNameToMoveTheRowTo = "Current";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
}
Trying to add an automatic timestamp to my spreadsheet. Anytime data is edited in Column "PM Status" I want an auto timestamp in column "Date Update".
function onEdit(event)
{
var timezone = "ET";
var timestamp_format = "MM-dd-yyyy"; // Timestamp Format.
var updateColName = "PM Status";
var timeStampColName = "Date Update";
var sheet = event.source.getSheetByName('All Leads'); //Name of the sheet where you want to run this script.
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName);
var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
function onEdit(event)
{
var sheet=event.source.getActiveSheet();
if(sheet.getName()=="All Leads")
{
var timezone = "ET";
var timestamp_format = "MM-dd-yyyy HH:mm:ss"; // Timestamp Format.
var updateColName = "PM Status";
var timeStampColName = "Date Update";
var actRng = event.range;
var editColumn = actRng.getColumn();
var actRow = actRng.getRow();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName) + 1;
var updateCol = headers[0].indexOf(updateColName) + 1;
if (dateCol > 0 && actRow > 1 && editColumn == updateCol)
{
var cell = sheet.getRange(actRow, dateCol);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
cell.setValue(date);
}
}
}
I am writing one of my first scripts, and have tried to look at other similar questions.
I have two sheets:
sheet1: new data (Only Column C is of interest, everything else can ignore in other columns)
sheet2: old data (but needs to be updated with sheet1 new data if not already there). The data to be added should be at the end of Column C after the existing data.
The code I have has the following compiling error.
I need to - get the last row of Column C Sheet2. Then check if Column C sheet1 is present in ColumnC sheet2, if not present- copy over from sheet1 to sheet2 column C.
UPDATED CODE:
function updateSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = "Sheet1";
var destinationSheet = "Sheet2";
var source_sheet = ss.getSheetByName(sourceSheet);
var target_sheet = ss.getSheetByName(destinationSheet);
var last_row = CountColC();
//assumes headers in row 1
var r = target_sheet.getRange(1,2, lastRow - 1);
//Note the use of an array
r.sort([{column: 3, ascending: true}]);
// Process sheet
_updateSpreadsheet(source_sheet, target_sheet);
}
//gets last row in Column C
function CountColC(){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for(var i = data.length-1 ; i >=0 ; i--){
if (data[i][2] != null && data[i][2] != ''){
return i+1 ;
}
}
}
function _updateSpreadsheet(source_sheet, target_sheet) {
var last_row = CountColC();
var source_data = source_sheet.getDataRange().getValues();
var target_data = target_sheet.getDataRange().getValues();
var resultArray = [];
for (var n = 1 ; n < source_data.length ; n++) {
var keep = true;
for(var p = 1 ; p < target_data.length ; p++) {
if (new Date(source_data[n][2]).getTime() == new Date(target_data[p][2]).getTime()) {
keep = false; break;
}
}
Logger.log(keep);
if(keep){ resultArray.push([source_data[n][2]])};
}
last_row++;
Logger.log(resultArray);
target_sheet.getRange(last_row,1,resultArray.length,resultArray[2].length).setValues(resultArray);
// target_data.push(n);
}
Thanks in advance :)
function updateSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = "Sheet1";
var destinationSheet = "Sheet2";
var source_sheet = ss.getSheetByName(sourceSheet);
var target_sheet = ss.getSheetByName(destinationSheet);
var lastCol = target_sheet.getLastColumn();
var lastRow = target_sheet.lastColumn();
//assumes headers in row 1
var r = target_sheet.getRange(2,1, lastRow - 1, 3);
//Note the use of an array
r.sort([{column: 3, ascending: true}]);
// Process sheet
_updateSpreadsheet(source_sheet, target_sheet);
}
//gets last row in Column C
function CountColC(){
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
for(var i = data.length-1 ; i >=0 ; i--){
if (data[i][2] != null && data[i][2] != ''){
return i+1 ;
}
}
}
function _updateSpreadsheet(source_sheet, target_sheet) {
var last_row = target_sheet.CountColC();
var source_data = source_sheet.getDataRange().getValues();
var target_data = target_sheet.getDataRange().getValues();
var resultArray = [];
for (var n = 1 ; n < source_data.length ; n++) {
var keep = true;
for(var p = 1 ; p < target_data.length ; p++) {
if (new Date(source_data[n][2]).getTime() == new Date(target_data[p][2]).getTime()) {
keep = false; break;
}
}
Logger.log(keep);
if(keep){ resultArray.push([source_data[n][2]])};
}
last_row++;
Logger.log(resultArray);
target_sheet.getRange(last_row,1,resultArray.length,resultArray[2].length).setValues(resultArray);
// target_data.push(n);
}
Instead call
var last_row = target_sheet.CountColC();
Please use
var last_row = CountColC();