简体   繁体   English

使用MSBuild更改.xla文件

[英]Change .xla File with MSBuild

I'm trying to create a build script for my current project, which includes an Excel Add-in. 我正在尝试为我当前的项目创建一个构建脚本,其中包括一个Excel加载项。 The Add-in contains a VBProject with a file modGlobal with a variable version_Number. 该加载项包含一个带有modGlobal文件的VBProject,其中包含一个变量version_Number。 This number needs to be changed for every build. 每个构建都需要更改此数字。 The exact steps: 确切的步骤:

  1. Open XLA document with Excel. 使用Excel打开XLA文档。
  2. Switch to VBEditor mode. 切换到VBEditor模式。 (Alt+F11) (ALT + F11)
  3. Open VBProject, entering a password. 打开VBProject,输入密码。
  4. Open modGlobal file. 打开modGlobal文件。
  5. Change variable's default value to the current date. 将变量的默认值更改为当前日期。
  6. Close & save the project. 关闭并保存项目。

I'm at a loss for how to automate the process. 我对如何自动化这个过程感到茫然。 The best I can come up with is an excel macro or Auto-IT script. 我能想到的最好的是excel宏或Auto-IT脚本。 I could also write a custom MSBuild task, but that might get... tricky. 我也可以写一个自定义的MSBuild任务,但这可能会变得棘手。 Does anyone else have any other suggestions? 还有其他人有其他建议吗?

An alternative way of handling versioning of an XLA file is to use a custom property in Document Properties. 处理XLA文件版本控制的另一种方法是在“文档属性”中使用自定义属性。 You can access and manipulate using COM as described here: http://support.microsoft.com/?kbid=224351 . 您可以按照此处所述访问和操作COM: http//support.microsoft.com/?kbid = 224351

Advantages of this are: 这样做的好处是:

  • You can examine the version number without opening the XLA file 您可以在不打开XLA文件的情况下检查版本号

  • You don't need Excel on your build machine - only the DsoFile.dll component 您的构建计算机上不需要Excel - 只需要DsoFile.dll组件

Another alternative would be to store the version number (possibly other configuration data too) on a worksheet in the XLA file. 另一种方法是在XLA文件的工作表上存储版本号(可能还有其他配置数据)。 The worksheet would not be visible to users of the XLA. XLA的用户不会看到工作表。 One technique I have used in the past is to store the add-in as an XLS file in source control, then as part of the build process (eg in a Post-Build event) run the script below to convert it to an XLA in the output directory. 我过去使用的一种技术是将加载项作为XLS文件存储在源代码控制中,然后作为构建过程的一部分(例如,在构建后事件中)运行下面的脚本以将其转换为XLA输出目录。 This script could be easily extended to update a version number in a worksheet before saving. 在保存之前,可以轻松扩展此脚本以更新工作表中的版本号。 In my case I did this because my Excel Add-in used VSTO, and Visual Studio doesn't support XLA files directly. 在我的情况下,我这样做是因为我的Excel加载项使用了VSTO,而Visual Studio不直接支持XLA文件。

'
'   ConvertToXla.vbs
'
'   VBScript to convert an Excel spreadsheet (.xls) into an Excel Add-In (.xla)
'
'   The script takes two arguments:
'
'   - the name of the input XLS file.
'
'   - the name of the output XLA file.
'
Option Explicit
Dim nResult
On Error Resume Next
nResult = DoAction
If Err.Number <> 0 Then 
    Wscript.Echo Err.Description
    Wscript.Quit 1
End If
Wscript.Quit nResult

Private Function DoAction()

    Dim sInputFile, sOutputFile

    Dim argNum, argCount: argCount = Wscript.Arguments.Count

    If argCount < 2 Then
        Err.Raise 1, "ConvertToXla.vbs", "Missing argument"
    End If

    sInputFile = WScript.Arguments(0)
    sOutputFile = WScript.Arguments(1)

    Dim xlApplication

    Set xlApplication = WScript.CreateObject("Excel.Application")
    On Error Resume Next 
    ConvertFileToXla xlApplication, sInputFile, sOutputFile
    If Err.Number <> 0 Then 
        Dim nErrNumber
        Dim sErrSource
        Dim sErrDescription
        nErrNumber = Err.Number
        sErrSource = Err.Source
        sErrDescription = Err.Description
        xlApplication.Quit
        Err.Raise nErrNumber, sErrSource, sErrDescription
    Else
        xlApplication.Quit
    End If

End Function

Public Sub ConvertFileToXla(xlApplication, sInputFile, sOutputFile)

    Dim xlAddIn
    xlAddIn = 18 ' XlFileFormat.xlAddIn

    Dim w
    Set w = xlApplication.Workbooks.Open(sInputFile,,,,,,,,,True)
    w.IsAddIn = True
    w.SaveAs sOutputFile, xlAddIn
    w.Close False
End Sub

I'm not 100% sure how to do exactly what you have requested. 我不是100%确定如何完全按照你的要求做。 But guessing the goal you have in mind there are a few possibilities. 但是猜测你想到的目标有几种可能性。

1) Make part (or all) of your Globals a separate text file that is distributed with the .XLA I would use this for external references such as the version of the rest of your app. 1)将您的Globals的一部分(或全部)作为与.XLA一起分发的单独文本文件,我将其用于外部引用,例如应用程序其余部分的版本。 Write this at build time and distribute, and read on the load of the XLA. 在构建时写下这个并分发,并阅读XLA的负载。

2) I'm guessing your writing the version of the main component (ie: the non XLA part) of your application. 2)我猜你正在写你的应用程序的主要组件的版本(即:非XLA部分)。 If this is tru why store this in your XLA? 如果这是真的,为什么要将它存储在你的XLA中? Why not have the main part of the app allow certain version of the XLA to work. 为什么不让应用程序的主要部分允许某些版本的XLA工作。 Version 1.1 of the main app could accept calls from Version 7.1 - 8.9 of the XLA. 主应用程序的1.1版可以接受来自XLA版本7.1 - 8.9的调用。

3) If you are just looking to update the XLA so it gets included in your version control system or similar (i'm guessing here) maybe just touch the file so it looks like it changed. 3)如果你只是想更新XLA,以便它包含在你的版本控制系统或类似的(我在这里猜测)可能只是触摸文件,使它看起来像是改变了。

If it's the version of the rest of the app that you are controlling i'd just stick it in a text file and distribute that along with the XLA. 如果它是您正在控制的应用程序其余部分的版本,我只需将其粘贴在文本文件中并与XLA一起分发。

You can modify the code in the xla programmatically from within Excel. 您可以在Excel中以编程方式修改xla中的代码。 You will need a reference to the 'Microsoft Visual Basic for Applications Extensibility..' component. 您将需要对“Microsoft Visual Basic for Applications Extensibility ..”组件的引用。

The examples on Chip Pearson's excellent site should get you started. Chip Pearson优秀网站上的例子可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM