Oct 3rd, 2021
A couple days ago when I was reviewing my notes to refresh my memory on some topics, I noticed that my notes were not stored in a single "notebook" - they were spreaded across multiple notetaking apps. This was because I could not settle on one notetaking application; I was struggling to find one that gives a good balance on all of my needs. As a result it quickly became quite difficult to find the specific notes I wanted to review.
Since I have been using Microsoft OneNote for notetaking purposes for awhile now, I decided to place all of my notes from other notetaking apps into Microsoft OneNote. But that sounds like a cumbersome and tedious job to do manually, doesn't it? So, after doing some googling on the best way to create Microsoft OneNote pages programmatically, I decided to create a custom, unpublished Microsoft OneNote add-in to do all of that work for me.
A slight note before we begin: You can only use your custom Microsoft OneNote add-in in the web version of Microsoft OneNote.
The process to create a Microsoft OneNote add-in project is well explained and documented in the official guide. Follow the steps described in the guide to create a simple quickstart Microsoft OneNote add-in project. This project will serve as our template to build our custom add-in.
Although it is not necessary for this guide, I highly recommend reading through the development lifecycle of Office add-ins to get a better understanding of how a Microsoft OneNote add-in works.
Once you have set up the quickstart add-in project, we can now proceed to modify it for our purpose.
First, we will need to add an input element in the taskpane HTML document.
taskpane.html<div> tag with id="run", add a new input element: <input type="file" id="file-selector">Now, we will need to add an event listener to that input element so it will parse the csv file and create a page for each record once you uploaded a csv file.
taskpane.tsrun button was selected, select the input element from the document using getElementById(..) on file-selector:input element with addEventListener(..) for the change event:addEventListener(..) a callback function called generatePageHandler. This will be called once a csv file is uploadedThe generatePageHandler function is where we will handle the process of parsing the file and generating the pages.
generatePageHandler function. Within it:FileReader instance to read the fileFileReader for the load evente.target.result. We will define the generateHTMLFromFile function later for parsing the csv file and creating HTML strings. We will also define the createPages function later for generating the pages for each record in the csv filereadAsText() function on the FileReader instanceThe generateHTMLFromFile function will return HTML strings from the csv file.
generateHTMLFromFile function. Within it:<p> tag around your content.The createPages function will create Microsoft OneNote pages with those HTML strings using Microsoft OneNote API.
createPages function. Within it:run function from the Office API. It should contain all interactions with Microsoft OneNotesection by calling the getActiveSection(..) method on context.applicationpage by calling addPage(..) on the section proxy objectpage to this new page by calling navigateToPage(..)addOutline(..) method on the page object to add your content into itcontext.sync() to sync Microsoft Office objects' statesYou can refer to the source code here.
npm run start:webInsert, select Office Add-insUpload My Add-in located next to the Refresh button. A new window should pop up asking you to upload your project's manifest.xmlmanifest.xml. Once that is done, you should be able to see your add-in located under Home in the toolbaro365devx. “Build Your First OneNote Task Pane Add-In.” Office Add-Ins | Microsoft Docs, 4 Aug. 2021, https://docs.microsoft.com/en-us/office/dev/add-ins/quickstarts/onenote-quickstart.
“Onenote Package - Office Add-Ins.” Office Add-Ins | Microsoft Docs, https://docs.microsoft.com/en-us/javascript/api/onenote?view=onenote-js-1.1.