Skip to content

Commit

Permalink
#151: export date and datetime format option
Browse files Browse the repository at this point in the history
  • Loading branch information
dufoli committed Jul 29, 2024
1 parent 3c7858b commit 8fbf5f0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
79 changes: 78 additions & 1 deletion addon/data-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -1646,17 +1646,55 @@ class Model {

function RecordTable(vm) {
let columnIdx = new Map();
let columnType = new Map();
let header = ["_"];
let skipTechnicalColumns = localStorage.getItem("skipTechnicalColumns") !== "false";
let dateFormat = localStorage.getItem("dateFormat");
let datetimeFormat = localStorage.getItem("datetimeFormat");
// try to respect the right order of column by matching query column and record column
function discoverQueryColumns(record, fields) {
let sobjectDescribe = null;
//TODO we will need parent model of rt maybe
if (record.attributes && record.attributes.type) {
let sobjectName = record.attributes.type;
//TODO maybe we will need to wait that cache is already filled on describe
sobjectDescribe = vm.describeInfo.describeSobject(vm.queryTooling, sobjectName).sobjectDescribe;
}
for (let field of fields) {
let fieldName = "";
let fieldType = "";
if (field.name) {
let fieldNameSplitted = field.name.split(".");
let subRecord = record;
let currentSobjectDescribe = sobjectDescribe;
for (let i = 0; i < fieldNameSplitted.length; i++) {
const currentFieldName = fieldNameSplitted[i];
// 1. try to collect name with describe
if (currentSobjectDescribe) {
let arr = currentSobjectDescribe.fields
.filter(sobjField => sobjField.relationshipName && sobjField.relationshipName.toLowerCase() == currentFieldName.toLowerCase())
.map(sobjField => (sobjField));
if (arr.length > 0) {
if (arr[0].ReferenceTo) {
//only take first ReferenceTo
currentSobjectDescribe = vm.describeInfo.describeSobject(vm.queryTooling, arr[0].ReferenceTo[0]).sobjectDescribe;
fieldName = fieldName ? fieldName + "." + arr[0].relationshipName : arr[0].relationshipName;
continue;
}
}
arr = currentSobjectDescribe.fields
.filter(sobjField => sobjField.name.toLowerCase() == currentFieldName.toLowerCase())
.map(sobjField => (sobjField));
if (arr.length > 0) {
fieldName = fieldName ? fieldName + "." + arr[0].name : arr[0].name;
fieldType = arr[0].type;
if (!columnType.has(fieldName)) {
columnType.set(fieldName, fieldType);
}
break;
}
}
// 2. try to collect name with record structure
for (let f in subRecord) {
if (f && currentFieldName && f.toLowerCase() == currentFieldName.toLowerCase()) {
subRecord = subRecord[f];
Expand Down Expand Up @@ -1724,12 +1762,51 @@ function RecordTable(vm) {
header[c] = column;
rt.colVisibilities.push(true);
}
row[c] = record[field];
if (columnType.get(field) == "date" && dateFormat) {
row[c] = convertDate(record[field], dateFormat);
} else if (columnType.get(field) == "datetime" && datetimeFormat) {
row[c] = convertDate(record[field], datetimeFormat);
} else {
row[c] = record[field];
}
if (typeof record[field] == "object" && record[field] != null) {
discoverColumns(record[field], column + ".", row);
}
}
}
function convertDate(field, format) {
let dt = new Date(field);
let formatedDate = "";
let remaining = format;
while (remaining) {
if (remaining.match(/^yyyy/i)) {
remaining = remaining.substring(4);
formatedDate += dt.getFullYear();
} else if (remaining.match(/^MM/)) {
remaining = remaining.substring(2);
formatedDate += ("0" + (dt.getMonth() + 1)).slice(-2);
} else if (remaining.match(/^dd/i)) {
remaining = remaining.substring(2);
formatedDate += ("0" + dt.getDate()).slice(-2);
} else if (remaining.match(/^HH/)) {
remaining = remaining.substring(2);
formatedDate += ("0" + dt.getHours()).slice(-2);
} else if (remaining.match(/^mm/)) {
remaining = remaining.substring(2);
formatedDate += ("0" + dt.getMinutes()).slice(-2);
} else if (remaining.match(/^ss/)) {
remaining = remaining.substring(2);
formatedDate += ("0" + dt.getSeconds()).slice(-2);
} else if (remaining.match(/^SSS/)) {
remaining = remaining.substring(3);
formatedDate += ("00" + dt.getMilliseconds()).slice(-3);
} else {
formatedDate += remaining[0];
remaining = remaining.substring(1);
}
}
return formatedDate;
}
function cellToString(cell) {
if (cell == null) {
return "";
Expand Down
2 changes: 2 additions & 0 deletions addon/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ class OptionsTabSelector extends React.Component {
{option: Option, props: {type: "toggle", title: "Display Query Execution Time", key: "displayQueryPerformance", default: true}},
{option: Option, props: {type: "toggle", title: "Use SObject context on Data Export", key: "useSObjectContextOnDataImportLink", default: true}},
{option: Option, props: {type: "toggle", title: "Skip technical comlumns", key: "skipTechnicalColumns", default: true}},
{option: Option, props: {type: "text", title: "Date format", key: "dateFormat", placeholder: "yyyy-MM-dd"}},
{option: Option, props: {type: "text", title: "Date time format", key: "datetimeFormat", placeholder: "yyyy-MM-ddTHH:mm:ss.SSS+/-HH:mm"}},
{option: QueryTemplatesOption, props: {title: "Query Templates", key: "queryTemplates", placeholder: "SELECT..."}},
{option: QueryTemplatesOption, props: {title: "Saved Query History", key: "insextSavedQueryHistory", node: "query", withName: true, defaultValue: "{\"useToolingApi\": false}", placeholder: "SELECT..."}}
]
Expand Down

0 comments on commit 8fbf5f0

Please sign in to comment.