1

我试图用 js 保存剑道电子表格数据。在解析行值时,我看到剑道电子表格将日期转换为数字,将时间转换为十进制。我已经使用 moment js 将数字转换为日期,但无法将十进制数字转换为确切的时间。如何转换它,或者如果我能得到电子表格中显示的日期和时间,那就太好了,这意味着只有字符串。下面是示例代码和屏幕截图。

var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
    var spreadsheedata = spreadsheet.toJSON();
    var JsonArray = [];
        for (var i = 1; i < spreadsheedata.sheets[0].rows.length; i++) {
                var dataObject = {};
                dataObject.SortOrder = spreadsheedata.sheets[0].rows[i].cells[0].value;
                dataObject.RoundNo = spreadsheedata.sheets[0].rows[i].cells[1].value;
                dataObject.Team = spreadsheedata.sheets[0].rows[i].cells[2].value;
                dataObject.MatchDate = moment(new Date((new Date('01/01/1900')).setDate(spreadsheedata.sheets[0].rows[i].cells[3].value - 1))).format(spreadsheedata.sheets[0].rows[i].cells[3].format.replace("dd", "DD").replace("mm", "MM"));
                dataObject.StartTime = spreadsheedata.sheets[0].rows[i].cells[4].value;
                dataObject.EndTime = spreadsheedata.sheets[0].rows[i].cells[5].value;
                dataObject.Location = spreadsheedata.sheets[0].rows[i].cells[6].value;
                dataObject.Field = spreadsheedata.sheets[0].rows[i].cells[7].value;
                JsonArray.push(dataObject);
    }

查看单元格编号格式和值 查看单元格格式

在此处输入图像描述 这是日期和时间字段

4

1 回答 1

0

使用函数 ConvertExcelTime 和 momentjs 转换 StartTime 和 EndTime。

var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
var spreadsheedata = spreadsheet.toJSON();
var JsonArray = [];
    for (var i = 1; i < spreadsheedata.sheets[0].rows.length; i++) {
            var dataObject = {};
            dataObject.SortOrder = spreadsheedata.sheets[0].rows[i].cells[0].value;
            dataObject.RoundNo = spreadsheedata.sheets[0].rows[i].cells[1].value;
            dataObject.Team = spreadsheedata.sheets[0].rows[i].cells[2].value;
            dataObject.MatchDate = moment(new Date((new Date('01/01/1900')).setDate(spreadsheedata.sheets[0].rows[i].cells[3].value - 1))).format(spreadsheedata.sheets[0].rows[i].cells[3].format.replace("dd", "DD").replace("mm", "MM").replace("-", "/").replace("-", "/"));
            dataObject.StartTime = moment(new Date(OrgTrainingCalendar.ConvertExcelTime(spreadsheedata.sheets[0].rows[i].cells[4].value))).format(spreadsheedata.sheets[0].rows[i].cells[4].format.replace("AM/PM", "a"));
            dataObject.EndTime = moment(new Date(OrgTrainingCalendar.ConvertExcelTime(spreadsheedata.sheets[0].rows[i].cells[5].value))).format(spreadsheedata.sheets[0].rows[i].cells[5].format.replace("AM/PM", "a"));
            dataObject.Location = spreadsheedata.sheets[0].rows[i].cells[6].value;
            dataObject.Field = spreadsheedata.sheets[0].rows[i].cells[7].value;
            JsonArray.push(dataObject);
}
ConvertExcelTime: function (excelTimestamp) {
    const secondsInDay = 24 * 60 * 60;
    const excelEpoch = new Date(1899, 11, 31);
    const excelEpochAsUnixTimestamp = excelEpoch.getTime();
    const missingLeapYearDay = secondsInDay * 1000;
    const delta = excelEpochAsUnixTimestamp - missingLeapYearDay;
    const excelTimestampAsUnixTimestamp = excelTimestamp * secondsInDay * 1000;
    const parsed = excelTimestampAsUnixTimestamp + delta;
    return isNaN(parsed) ? null : parsed;
}
于 2021-02-18T10:20:09.100 回答