Convert dates to a format Excel can process

This commit is contained in:
kurtb 2016-10-16 14:33:33 -07:00
Родитель 178f3f8719
Коммит ad8ae667c5
4 изменённых файлов: 61 добавлений и 26 удалений

Просмотреть файл

@ -25,11 +25,17 @@ export interface ITableListener {
rowsSelected(rows: any[]): Promise<void>;
}
export interface ITableColumn {
name: string;
format: string;
}
/**
* Table access interface
*/
export interface ITable {
loadData(columns: string[], rows: any[]): Promise<void>;
loadData(columns: ITableColumn[], rows: any[]): Promise<void>;
addListener(listener: ITableListener): Promise<void>;
}

Просмотреть файл

@ -4,6 +4,7 @@ import { Promise } from 'es6-promise';
import { pnhost, IEchoService, EchoServiceName, ITable, ITableService, TableServiceName, ITableListener } from '../api/index';
var fullcalendar = require('fullcalendar');
var qtip = require('qtip2');
import * as moment from 'moment';
class TableListener implements ITableListener {
constructor(private _viewModel: CalendarViewModel) {
@ -109,9 +110,20 @@ class CalendarViewModel {
}
this._tableP.then((table) => {
let columns = ['provider', 'title', 'location', 'start', 'end', 'responseStatus'];
// Longer term we should standardize on some format here so there isn't a disconnect between Office and moment
const columnTimeFormatString = "m/d/yy h:mm AM/PM";
let columns = [
{ name: 'id', format: null },
{ name: 'provider', format: null },
{ name: 'title', format: null },
{ name: 'location', format: null },
{ name: 'start', format: columnTimeFormatString },
{ name: 'end', format: columnTimeFormatString },
{ name: 'responseStatus', format: null }];
// Get and store the rows we will bind to the pnhost table
// Get and store the rows we will bind to the pnhost table - we convert times to a format easier to parse by hosts (i.e. Excel)
const formatString = "M/D/YY h:mm A";
this._tableBoundRows = [];
for (let calendar of this._cachedCalendars) {
for (let event of calendar.events) {
@ -120,8 +132,8 @@ class CalendarViewModel {
provider: calendar.sourceName,
title: event.title,
location: event.location,
start: event.start,
end: event.end,
start: moment(event.start).format(formatString),
end: moment(event.end).format(formatString),
responseStatus: event.responseStatus,
self: event.self
})

Просмотреть файл

@ -2,7 +2,7 @@ import { Component, Input, OnChanges, SimpleChanges, OnInit } from '@angular/cor
import { InteractiveDocumentViewService } from './interactive-document-view.service';
import { InteractiveDocumentService } from './interactive-document.service';
import { ViewModel, IViews, IView, Resource } from '../interfaces';
import { PostMessageHostServer, EchoServiceName, TableServiceName, ITableService, ITable, ITableListener } from '../api/index';
import { PostMessageHostServer, ITableColumn, EchoServiceName, TableServiceName, ITableService, ITable, ITableListener } from '../api/index';
import * as services from '../services/index';
import {
Angular2DataTableModule,
@ -24,9 +24,9 @@ class Table implements ITable {
selection: any[] = [];
private _listeners: ITableListener[] = [];
loadData(columns: string[], rows: any[]): Promise<void> {
loadData(columns: ITableColumn[], rows: any[]): Promise<void> {
console.log('load data');
let columnOptions = columns.map((column) => new TableColumn({prop: column}));
let columnOptions = columns.map((column) => new TableColumn({prop: column.name}));
// TODO There's a bug in the angular table where changing column options adds extra padding
// to the table. So assuming the columns don't change between loads for now.

Просмотреть файл

@ -3,7 +3,7 @@
<link href="/stylesheets/embed.css" rel="stylesheet" />
{{/styles}}
{{$content}}
{{$content}}
<iframe src="/documents/calendar" frameborder="0" style="overflow:hidden;height:100%;width:100%" height="100%" width="100%"></iframe>
{{/content}}
@ -28,19 +28,26 @@
this._listeners = [];
}
Table.prototype.notifyTableUpdate = function(newRowsRaw) {
Table.prototype.notifyTableUpdate = function(newRowsRaw) {
// find which column has the id field
var idColumn = -1;
for (var i = 0; i < this._columns.length; i++) {
if (this._columns[i].name === 'id') {
idColumn = i;
break;
}
}
if (idColumn === -1) {
throw "no id column defined";
}
// Run through the newRows and use that to filter the existing _rows
var newRows = [];
for (var i = 0; i < this._rows.length; i++) {
// See if the row exists
for (var j = 1; j < newRowsRaw.value.length; j++) {
var match = true;
for (var k = 0; k < this._columns.length; k++) {
if (this._rows[i][this._columns[k]] !== newRowsRaw.value[j][k]) {
match = false;
}
}
if (match) {
for (var j = 1; j < newRowsRaw.value.length; j++) {
if (this._rows[i]['id'] === newRowsRaw.value[j][idColumn]) {
newRows.push(this._rows[i]);
}
}
@ -80,7 +87,9 @@
Table.prototype.loadData = function (columns, rows) {
var that = this;
this._columns = columns;
this._rows = rows;
this._rows = rows;
var columnFormats = columns.map(function(column) { return column.format });
var columnNames = columns.map(function(column) { return column.name });
// Run a batch operation against the Excel object model
Excel.run(function (ctx) {
@ -96,9 +105,13 @@
for (var i = 0; i < rows.length; i++) {
var newRow = [];
for (var j = 0; j < columns.length; j++) {
newRow.push(rows[i][columns[j]]);
newRow.push(rows[i][columns[j].name]);
}
tableRows.add(null, [newRow]);
var row = tableRows.add();
var range = row.getRange();
range.numberFormat = [columnFormats];
range.values = [newRow];
}
});
}
@ -112,19 +125,23 @@
var table = sheet.tables.add('A1:' + endPosition, true);
that.table = table;
table.name = "calendarTable";
table.getHeaderRowRange().values = [columns];
table.getHeaderRowRange().values = [columnNames];
var rowsToLoad = [];
for (var i = 0; i < rows.length; i++) {
var newRow = [];
for (var j = 0; j < columns.length; j++) {
newRow.push(rows[i][columns[j]]);
newRow.push(rows[i][columns[j].name]);
}
table.rows.add(null, [newRow]);
var row = table.rows.add();
var range = row.getRange();
range.numberFormat = [columnFormats];
range.values = [newRow];
}
//Run the queued commands, and return a promise to indicate task completion
return ctx.sync().then(function() {
return ctx.sync().then(function() {
//Create a new table binding for myTable
Office.context.document.bindings.addFromNamedItemAsync(
"calendarTable",