Code Monkey home page Code Monkey logo

csharp-excel-vba-sync's Introduction

C# Excel VBA Sync

C# library to support importing and exporting VBA code from an Excel workbook

How to Use

The main entry point to the library is the ExcelVbaSyncApi singleton class. From that class you can get a VBA importer or exporter for an Excel Workbook object (Microsoft.Office.Interop.Excel.Workbook).

There is an example CLI project included in this repository to show how to use this library.

Export

The exporter class allows exporting VBA code from a given workbook to a folder on you local machine.

IExcelVbaExporter exporter = ExcelVbaSyncApi.Instance.NewVbaExporter(workbook);
exporter.Export(targetDirectoryPath);

You can also filter what VBA components you want to export by providing a filter function.

exporter.Export(targetDirectoryPath, component => component.Name.Contains("Export"));

The exporter class can be re-used multiple times to export the VBA code to different folders or use different filters.

exporter.Export(firstPath, component => component.Name.StartsWith("A_"));
exporter.Export(secondPath, component => component.Name.StartsWith("B_"));

Import

The import functionality will import VBA code into the given workbook and replace the code that is currently there. This expects the VBA code to be in the same format that the export functionality generates.

IExcelVbaImporter importer = ExcelVbaSyncApi.Instance.NewVbaImporter(workbook);
importer.Import(sourceDirectoryPath);
importer.RemoveComponentsThatWereNotImported();

Similar to the export functionality it is possible to provide a filter to choose which files get imported.

importer.Import(sourceDirectoryPath, fileName => fileName.Contains("Import"));

The importer can also be used to import VBA code multiple times from different folders or using different filters.

importer.Import(firstPath, fileName => fileName.StartsWith("A_"));
importer.Import(secondPath, fileName => fileName.StartsWith("B_"));

RemoveComponentsThatWereNotImported

The importer has an extra function: RemoveComponentsThatWereNotImported(). The purpose of this function is to clean up the existing code in the Workbook by removing VBA components that were not imported. The purpose is to have the VBA code in the workbook mirror the VBA code in the folder it was imported from.

For example, if the Workbook contains the following components: A, B, and C, but only the A and B components were imported, this function will remove component C.

This makes sense when using this library to version control VBA code in a workbook, because after an import you will want the VBA code in the workbook to always mirror the VBA code in the source control folder.

csharp-excel-vba-sync's People

Contributors

hectorjsmith avatar

Stargazers

 avatar Jason Kerney avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.