51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 14527|回复: 4
打印 上一主题 下一主题

TortoiseSVN中Word、Excel比较差异出错解决方法

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2010-3-26 09:31:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
无意中,发现比较Word、Excel主要只是用到了安装目录下的两个比较差异的脚本,如果提示找不到两个文件,只要把相应的代码放到这两个脚本里,就可以比较了!

制作方法:新建记事本,将下面对应的代码复制进去,另存为相应的脚本文件即可(路径为安装TortoiseSVN的路径下的:Diff-Scripts 目录);

一个是Word的,存成:diff-doc.js   内容为:

var objArgs,num,sBaseDoc,sNewDoc,objScript,word,destination;
// Microsoft Office versions for Microsoft Windows OS
var vOffice2000 = 9;
var vOffice2002 = 10;
var vOffice2003 = 11;
var vOffice2007 = 12;
// WdCompareTarget
var wdCompareTargetSelected = 0;
var wdCompareTargetCurrent = 1;
var wdCompareTargetNew = 2;
// WdViewType
var wdMasterView = 5;
var wdNormalView = 1;
var wdOutlineView = 2;
// WdSaveOptions
var wdDoNotSaveChanges = 0;
var wdPromptToSaveChanges = -2;
var wdSaveChanges = -1;

objArgs = WScript.Arguments;
num = objArgs.length;
if (num < 2)
{
   WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");
   WScript.Quit(1);
}

sBaseDoc = objArgs(0);
sNewDoc = objArgs(1);

objScript = new ActiveXObject("Scripting.FileSystemObject" );
if ( ! objScript.FileExists(sBaseDoc))
{
    WScript.Echo("File " + sBaseDoc + " does not exist.  Cannot compare the documents.");
    WScript.Quit(1);
}
if ( ! objScript.FileExists(sNewDoc))
{
    WScript.Echo("File " + sNewDoc + " does not exist.  Cannot compare the documents.");
    WScript.Quit(1);
}

objScript = null;

try
{
   word = WScript.CreateObject("Word.Application") ;
}
catch(e)
{
   WScript.Echo("You must have Microsoft Word installed to perform this operation.");
   WScript.Quit(1);
}

word.visible = true;

// Open the new document
destination = word.Documents.Open(sNewDoc) ;

// If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
if (((destination.ActiveWindow.View.Type == wdOutlineView) || (destination.ActiveWindow.View.Type == wdMasterView)) && (destination.Subdocuments.Count == 0))
{
    // Change the Type property of the current document to normal
    destination.ActiveWindow.View.Type = wdNormalView;
}

// Compare to the base document
if (Number(word.Version) <= vOffice2000)
{
    // Compare for Office 2000 and earlier
    destination.Compare(sBaseDoc);
}
else
{
    // Compare for Office XP (2002) and later
    destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
}
   
// Show the comparison result
if (Number(word.Version) < vOffice2007)
{
    word.ActiveDocument.Windows(1).Visible = 1;
}
   
// Mark the comparison document as saved to prevent the annoying
// "Save as" dialog from appearing.
word.ActiveDocument.Saved = 1;
   
// Close the first document
if (Number(word.Version) >= vOffice2002)
{
    destination.Close(wdDoNotSaveChanges);
}





还有一个是Excel的,存成:diff-xls.vbs



dim objExcelApp, objArgs, objScript, objBaseDoc, objNewDoc, objWorkSheet, i

Set objArgs = WScript.Arguments
num = objArgs.Count
if num < 2 then
   MsgBox "Usage: [CScript | WScript] compare.vbs base.doc new.doc", vbExclamation, "Invalid arguments"
   WScript.Quit 1
end if

sBaseDoc = objArgs(0)
sNewDoc = objArgs(1)

Set objScript = CreateObject("Scripting.FileSystemObject")
If objScript.FileExists(sBaseDoc) = False Then
    MsgBox "File " + sBaseDoc +" does not exist.  Cannot compare the documents.", vbExclamation, "File not found"
    Wscript.Quit 1
End If
If objScript.FileExists(sNewDoc) = False Then
    MsgBox "File " + sNewDoc +" does not exist.  Cannot compare the documents.", vbExclamation, "File not found"
    Wscript.Quit 1
End If

Set objScript = Nothing

On Error Resume Next
Set objExcelApp = Wscript.CreateObject("Excel.Application")
If Err.Number <> 0 Then
   Wscript.Echo "You must have Excel installed to perform this operation."
   Wscript.Quit 1
End If

'Open base excel sheet
objExcelApp.Workbooks.Open sBaseDoc
'Open new excel sheet
objExcelApp.Workbooks.Open sNewDoc
'Show Excel window
objExcelApp.Visible = True
'Create a compare side by side view
objExcelApp.Windows.CompareSideBySideWith(objExcelApp.Windows(2).Caption)
If Err.Number <> 0 Then
        objExcelApp.Application.WindowState = xlMaximized
        objExcelApp.Windows.Arrange(-4128)
End If

'Mark differences in sNewDoc red
i = 1

objExcelApp.Workbooks(1).screenUpdating=False
objExcelApp.Workbooks(2).screenUpdating=False

For Each objWorkSheet In objExcelApp.Workbooks(2).Worksheets

                objworksheet.Cells.FormatConditions.Delete

                objExcelApp.Workbooks(1).Sheets(i).Copy ,objExcelApp.Workbooks(2).Sheets(i*2-1)

                objExcelApp.Workbooks(2).Sheets(i*2).Name = "Dummy_for_Comparison" & i

                'objworksheet.Activate
                'To create a local formula the cell A1 is used
                original_content = objworksheet.Cells(1,1).Formula
                String sFormula
                'objworksheet.Cells(1,1).Formula = "=INDIRECT(""" & objExcelApp.Workbooks(2).Sheets(i*2).name & " (2)"& "!""&ADDRESS(ROW(),COLUMN()))"
                objworksheet.Cells(1,1).Formula = "=INDIRECT(""Dummy_for_Comparison" & i & "!""&ADDRESS(ROW(),COLUMN()))"
                sFormula = objworksheet.Cells(1,1).FormulaLocal

                objworksheet.Cells(1,1).Formula = original_content
                'with the local formula the conditional formatting is used to mark the cells that are different
                const xlCellValue = 1
                const xlNotEqual = 4
                objworksheet.Cells.FormatConditions.Add xlCellValue, xlNotEqual, sFormula
                objworksheet.Cells.FormatConditions(1).Interior.ColorIndex = 3

        i = i + 1
next

objExcelApp.Workbooks(1).close
objExcelApp.Workbooks(2).screenUpdating=True
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2010-3-31 15:25:14 | 只看该作者
感谢分享
回复 支持 反对

使用道具 举报

  • TA的每日心情
    奋斗
    2022-5-8 19:23
  • 签到天数: 137 天

    连续签到: 1 天

    [LV.7]测试师长

    3#
    发表于 2010-4-28 23:47:23 | 只看该作者
    多谢分享,收藏
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    4#
    发表于 2010-7-13 09:22:14 | 只看该作者
    楼主好人,谢谢分享
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    5#
    发表于 2010-10-15 11:16:44 | 只看该作者
    这个不错,谢谢了
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-22 07:00 , Processed in 0.089633 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表