Spreadsheet On load event issues

Tags: #<Tag:0x00007efc6ddfb478>

Below my code it is working on start date changes. But I want to spreadsheet onload automatically change the records. How to do it.

function stringToDate(_date,_format,_delimiter){
var formatLowerCase=_format.toLowerCase();
var formatItems=formatLowerCase.split(_delimiter);
var dateItems=_date.split(_delimiter);
var monthIndex=formatItems.indexOf(“mm”);
var dayIndex=formatItems.indexOf(“dd”);
var yearIndex=formatItems.indexOf(“yyyy”);
var month=parseInt(dateItems[monthIndex]);
month-=1;
var formatedDate = new Date(dateItems[yearIndex],month,dateItems[dayIndex]);
return formatedDate;
}

Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};

function onSpreadsheetChange(changes, source) {
var hot = FormUtil.getField(“spreadsheetGrid”).data(“hot”); }

if (source === 'loadData' || source === 'internal') {
    return;
}
var fieldChanged = changes[0][1];
var detectionFields = ['start_date', "noofdays"];
//this block is important to stop processing (infinite loop) if field changed is not on watch list as setDataAtRowProp method down the line would trigger this method again.
if(!detectionFields.includes(fieldChanged)){
    return;
}
//Values of detected changed row and field
var rowNumber = changes[0][0];
//var fieldId = changes[0][1];
//var previousValue = changes[0][2];
//var currentValue = changes[0][3];
//Spreadsheet field IDs
var totalDaysField = "totaldays";
var startDateField = "start_date";
var endDateField = "end_date";
var startTimeField = "start_time";
var endTimeField = "end_time";
var createdByField = "current_user";
//Get values from spreadsheet cells
var totalDays = hot.getDataAtRowProp(rowNumber, totalDaysField);
var startDate = hot.getDataAtRowProp(rowNumber, startDateField);
//if both fields have values
if (totalDays && startDate) {
}
//pick up start date
var startDateV = stringToDate(startDate,"dd/MM/yyyy","/");
var endDateV   = startDateV;
//calculate end date
endDateV.addDays(parseInt(totalDays));
//convert date format
var endDate = $.datepicker.formatDate('dd/mm/yy', endDateV);
//set value to spreadsheet
hot.setDataAtRowProp(rowNumber, endDateField, endDate);

//Event changes*********
{
“afterInit” : function() {
var hot = this;
$(hot.rootElement).data(“hot”, hot);
},
afterChange: onSpreadsheetChange
}

Hi @djangopythoner

did you try to use the validateCells() core method after the instance is initialized? When the following method is called cells are validated and they are reformatted as it is provided in the cells/columns settings.

Hi @djangopythoner

How’s going? Did the validateCells call work as desired?