npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

excel-datamashup

v1.0.6

Published

Read and edit the Power Query formula in Excel documents.

Downloads

10

Readme

excel-datamashup

This sample project contains code to convert a Excel customXml item1.xml file into a usable data structure.

The various Excel formats xlsx, xlsm, xlsb are ZIP based and thus can be extracted.

The contents will in situations where Power Query is used, contain a customXml folder with a item1.xml file that contains the relevant data structure in binary format.

This binary format can be processed into something we can read, edit, then re-package back into binary format.

The goal of this project is to faciliate processing a Excel file, then being able to edit and save it in both browser and terminal modes.

API

You can work with the library in either ExcelZip mode which provides a certain level of wrapper for you.

import { type UnzippedExcel, ExcelZip } from 'excel-datamashup';

// read and store the binary zip data as number array, Uint8Array or Buffer
const zip = new Uint8Array();

// process the zip into a more manageable object
const excelZip: UnzippedExcel = await ExcelZip(zip);

// the object has some helper methods along with the raw data for manipulation
const originalFormula: string = excelZip.getFormula();

// modify or replace the formula entirely
const newFormula: string = originalFormula.replace('"Some Text"', '"New Text"');

// replace the formula with your new one
// this method will also call the internal `excelZip.datamashup.result.resetPermissions()` method for you
excelZip.setFormula(newFormula);

// zip the contents back to an Excel file
const newZip: Buffer = await excelZip.save();

The more direct approach is to simply focus on processing the DataMashup XML file directly.

import { ParseXml } from 'excel-datamashup';

// extract the contents of `customXml\item1.xml` using your favorite zip editing library
const xml: string = `...`;

// returns `ParseResult` object or a string corresponding to the `ParseError` type
const result: ParseResult = await ParseXml(xml);

// the object has some helper methods along with the raw data for manipulation
const originalFormula: string = result.getFormula();

// edit the original or create a entirely new power query
const newFormula: string = `...`;

// replace the formula with your new one
result.setFormula(newFormula);

// always reset permissions when editing
result.resetPermissions();

// update the contents of `customXml\item1.xml` by writing this new content using your favorite zip editing library
const newXml: string = await result.save();

Sample

The sample folder contain an example file that contains a Power Query. It simply outputs text to a table.

If you have this project checked out, simply uncomment the src\index.ts line 10 to enable the demo behavior.

Build the project and run dist\index.js in a browser, it will ask for you to upload a Excel file, which it will edit then download.

$inputFile = ".\sample\demo.xlsb"
$outputFile = ".\sample\demo.json"
$zipName = "customXml/item1.xml"
$tempFile = [System.IO.Path]::GetTempFileName()
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip = [System.IO.Compression.ZipFile]::OpenRead($inputFile)
try {
    $entry = $zip.Entries | Where-Object { $_.FullName -eq $zipName }
    $stream = $entry.Open()
    try {
        $reader = [IO.StreamReader]::new($stream)
        $writer = [IO.StreamWriter]::new($tempFile)
        try {
            while (-not $reader.EndOfStream) {
                $line = $reader.ReadLine()
                $writer.WriteLine($line)
            }
        } finally {
            $writer.Close()
            $reader.Close()
        }
    } finally {
        $stream.Close()
    }
} finally {
    $zip.Dispose()
}
(Get-Content -Path $tempFile) -join "" | ConvertTo-Json -Compress | Set-Content -Path $outputFile
Remove-Item -Path $tempFile

Resources

  • https://bengribaudo.com/blog/2020/04/22/5198/data-mashup-binary-stream
  • https://learn.microsoft.com/en-us/openspecs/office_file_formats/ms-qdeff/27b1dd1e-7de8-45d9-9c84-dfcc7a802e37