Files
wekan/models/lib/apiImport/onenote.js
T
Lauri Ojansivu 01e88410cd Import and export Azure DevOps Boards, Microsoft Lists, Project, Outlook, Teams and OneNote.
Azure DevOps Boards: a query's "Export to CSV" is imported (Board Column or
State as lists, Board Lane as swimlanes, Title 1/2/3 tree levels as parent
cards, "Name <address>" people, regional dates, other columns as custom
fields), and a board exports as the CSV "Import from CSV" reads.

Microsoft Lists: a list's "Export to CSV" is imported (Status or Progress as
the lists, people, dates, tags, other columns as custom fields), and a board
exports as a CSV that "New list - From CSV" makes a list of.

Microsoft Project: the "XML Format" (MSPDI) plan is imported (summary tasks
as swimlanes, progress as lists, subtasks, resources, predecessors), read by
a small XML reader that never resolves a DTD or an entity, and a board
exports as a plan Project opens.

Outlook: .msg files are read without a library - the Compound File Binary
format (MS-CFB) and the message's properties (MS-OXMSG) - into the mail
import, threads and attachments included, alone or in a .zip with .eml
files; a board exports as .eml files Outlook opens. The reader was checked
against .msg files saved by Outlook.

Teams and OneNote: fetched from Microsoft Graph with the member's token
(chats and channels; notebooks with their pages' HTML, to-do boxes as a
checklist), or read from a file - Teams' personal export .tar, Graph chat
messages, and the files WeKan exports: Teams chats as Graph chat messages,
OneNote pages as Graph's create-page XHTML in a .zip.

All six are under the Microsoft heading of Import and Export, with tests,
negative tests, live tests for the two Graph imports, documentation and the
import instruction in every language. Low confidence translations include
ace, ak, ay, bm, bo, bua, chr, csb, cv, dz, ee, ff, fj, gn, gv, haw, ig, iu,
kl, ks, kok, kw, ht, hsb, fur, fo, ga, gd, ku, lld, nah, nap, rup, sah, sc,
scn, se, nd, nso, ss, om, rn, rw, sm, sn, qu, pap, lg, mg, mi, ny, so, mai,
sd, ps, or_IN, my, si, rm, la, lb, tig, tlh, uz-AR, ve, ve-PP, ve-CC, vo, wa,
wal, wo, zgh, to, tpi, wa-RR and ts.

Thanks to xet7 !
2026-11-21 01:33:59 +02:00

53 lines
3.1 KiB
JavaScript

// Microsoft OneNote through Microsoft Graph (models/lib/apiImportServices.js):
// the member's notebooks, and one notebook's sections and pages with each
// page's HTML, as the JSON models/lib/onenoteApiFormat.js reads. Plain Node;
// `request` is passed in.
//
// The token is a Microsoft Graph access token with Notes.Read (Graph
// Explorer shows one). It goes only into the Authorization header of
// requests to graph.microsoft.com.
import { GRAPH, graphHeaders, graphAll } from './graph.js';
export const MAX_ONENOTE_REQUESTS = 5000;
export const MAX_ONENOTE_PAGES = 20000;
const id = value => encodeURIComponent(String(value));
export async function list(credentials, request) {
const headers = graphHeaders(credentials);
const notebooks = await graphAll(`${GRAPH}/me/onenote/notebooks`, headers, request, { pages: 0 },
{ what: 'OneNote notebooks', max: MAX_ONENOTE_REQUESTS });
return notebooks.filter(n => n && typeof n.id === 'string' && n.id)
.map(n => ({ id: n.id, name: String(n.displayName || n.id) }));
}
export async function fetch(credentials, notebookId, request) {
const headers = graphHeaders(credentials);
if (typeof notebookId !== 'string' || !notebookId) throw new Error('Choose what to import');
const state = { pages: 0 };
const limit = { max: MAX_ONENOTE_REQUESTS };
const notebooks = await graphAll(`${GRAPH}/me/onenote/notebooks`, headers, request, state, { what: 'OneNote notebooks', ...limit });
const notebook = notebooks.find(n => n && n.id === notebookId);
if (!notebook) throw new Error('OneNote has no such notebook');
const sections = await graphAll(`${GRAPH}/me/onenote/notebooks/${id(notebookId)}/sections`, headers, request, state,
{ what: 'OneNote sections', ...limit });
let total = 0;
const out = [];
for (const section of sections.filter(s => s && typeof s.id === 'string')) {
const pages = await graphAll(`${GRAPH}/me/onenote/sections/${id(section.id)}/pages?$select=id,title,level,order,createdDateTime`,
headers, request, state, { what: 'OneNote pages', ...limit });
total += pages.length;
if (total > MAX_ONENOTE_PAGES) throw new Error(`OneNote has more than ${MAX_ONENOTE_PAGES} pages in this notebook`);
const withContent = [];
for (const page of pages.filter(p => p && typeof p.id === 'string')) {
state.pages += 1;
if (state.pages > MAX_ONENOTE_REQUESTS) throw new Error(`more than ${MAX_ONENOTE_REQUESTS} requests to OneNote`);
// The page's HTML: the request function answers text that is not JSON as a string.
const content = await request(`${GRAPH}/me/onenote/pages/${id(page.id)}/content`, { method: 'GET', headers: { ...headers, Accept: 'text/html' } });
withContent.push({ id: page.id, title: String(page.title || ''), level: Number(page.level) || 0, order: Number(page.order) || 0,
createdDateTime: page.createdDateTime || '', content: typeof content === 'string' ? content : '' });
}
out.push({ id: section.id, displayName: String(section.displayName || ''), pages: withContent });
}
return { onenote: true, notebooks: [{ id: notebook.id, displayName: String(notebook.displayName || ''), sections: out }] };
}