预计& # 39;;& # 39;当尝试运行excel宏与jscript
Expected ';' when trying to run excel macro with jscript
我正在编写一个网页,作为访问为某些硬件生成配置文件的excel工作簿的前台。目前这只是我测试的概念,并熟悉如何jscript自动化excel。
我的问题是,当我试图运行宏,我不断得到一个"预期的';'错误在第46行Char 7。据我所知,语法是正确的,它与不同的excel工作簿宏一起工作。我已经在我的PC上修复了。dll并检查了IE设置,但让我困惑的是为什么这不会工作,而其他jscript运行得很好。
Works Fine: oXL.Run("ButtonTest.xlsm!Module1.buttonclick");
给出错误:oXL.Run("test.xlsm!Module1.makeconfigs");
概念测试的完整代码:
<!DOCTYPE html>
<html lang="en">
<body>
<SCRIPT LANGUAGE="VBScript">
</SCRIPT>
<SCRIPT LANGUAGE="JScript">
function AutomateExcel(store,direct,MdfFloor,MdfSW,Include)
{
// Start Excel and get Application object.
var oXL = new ActiveXObject("Excel.Application");
var filename = "D:''Profiles''ngwx36''Desktop''test.xlsm";
oXL.Visible = true;
// Open Staging Workbook
var oWB = oXL.Workbooks.Add(filename);
// Place vars from input in correct cell
oWB.Sheets("Instructions").Cells(1, 5).Value = store;
oWB.Sheets("Instructions").Cells(2,5).Value = direct;
oWB.Sheets("SWInventory").Cells(3,2).Value = MdfFloor;
oWB.Sheets("SWInventory").Cells(3,6).Value = MdfSW;
//checks to see if 3rd MDF needs to be included
if (Include == "Yes"){
oWB.Sheets("SWInventory").Cells(5,2).Value = "Included";
}
//fill 2 IDFs in to test atm
oWB.Sheets("SWInventory").Cells(7,2).Value = "1";
oWB.Sheets("SWInventory").Cells(7,3).Value = "1";
oWB.Sheets("SWInventory").Cells(7,4).Value = "SW01";
oWB.Sheets("SWInventory").Cells(7,6).Value = "EX2200C";
oWB.Sheets("SWInventory").Cells(8,2).Value = "2";
oWB.Sheets("SWInventory").Cells(8,3).Value = "2";
oWB.Sheets("SWInventory").Cells(8,4).Value = "SW02";
oWB.Sheets("SWInventory").Cells(8,6).Value = "EX2200C";
window.alert("Filled Sheet Just Fine");
//run config macro
oXL.Run("test.xlsm!Module1.makeconfigs");
window.alert("Process Complete");
}
</SCRIPT>
<Form Name=Input>
<p>
<label>Store Name</label>
<input type = "text"
name= "StoreName"
value = "" />
</p>
<p>
<label>File Directory</label>
<input type = "text"
name= "FilePath"
value = "" />
</p>
<p>
<label>MDF Floor #</label>
<input type = "text"
name= "MdfFloor"
value = "" />
</p>
<p>
<label>MDF Type</label>
<input type = "text"
name= "MdfType"
value = "Enter MDF SW TYpe" />
</p>
<p>
<label>MDF Include</label>
<input type = "text"
name= "MdfInc"
value = "3rd MDF Yes or No?" />
</p>
</form>
<P><INPUT id=button1 type=button value="Start Excel"
onclick="AutomateExcel Input.StoreName.Value,Input.FilePath.Value,Input.MdfFloor.Value,Input.MdfType.value,Input.MdfInc.Value">
</P>
</body>
</html>
更新:我还没有发现为什么我得到预期的错误,但我确实实现了一个解决方案,使一个VBScript函数,简单地运行宏修复。由于某些原因,VB可以运行这个特定的宏,但Jscript不喜欢。
<!DOCTYPE html>
<html lang="en">
<body>
<script language = "VBscript">
function RunMacro()
dim oXL
Set oXL = GetObject(,"Excel.Application")
oXL.Run "makeconfigs"
end function
</script>
<Script Language = "jscript">
function AutomateExcel(){
var oXL = new ActiveXObject("Excel.Application");
var filename = "D:''Profiles''ngwx36''Desktop''test.xlsm";
oXL.Visible = true;
var oWB = oXL.Workbooks.Add(filename);
RunMacro();
}
</Script>
<P><INPUT id=button1 type=button value="Start Excel"
onclick="AutomateExcel()">
</P>
</body>
</html>
这是一个错误代码冲突的情况。
JavaScript引擎将1004映射到*Syntax Errors: expected ';'*,参见JavaScript语法错误
Office Automation将代码1004映射到运行时错误1004:应用程序定义的或对象定义的错误
如果你用JavaScript调用Office组件(在你的例子中是Excel.Application),在组件中发生的运行时错误1004会传播到JavaScript引擎,JavaScript引擎会将此代码映射为语法错误:expected ';'。
当然,这种类型的错误传播是非常愚蠢的,感谢微软。
所以你的问题不在于JavaScript,而在于对Office组件的调用,大概是run()方法。
我看到的唯一一行缺少分号是您对window.alert()
的调用:
window.alert("Filled Sheet Just Fine")
把它加回去,我想你会没事的。
这行末尾少了一个分号:
window.alert("Filled Sheet Just Fine")
看起来JScript正在处理onclick
。它的语法适用于VBScript,但不适用于JScript。这个问题可以通过添加一个伪协议来解决:
onclick="VBScript: AutomateExcel Input.StoreName.Value, Input.FilePath.Value, ... "
JScript expects ; here ---^
您也可以使用JScript,但随后您需要将AutomateExcel()
的参数括在括号中,虽然我不确定Input
是否为JScript定义。
我不清楚,为什么在线事件处理程序有时用VBScript解释,有时用JScript解释。也许你想问一个新的问题。
//这对我来说很好。
function call_Macro(){
try{
var ExApp;
var excel_file;
ExApp = new ActiveXObject("Excel.Application");
var excel_file = ExApp.Workbooks.Open("D:''NewFolder''FSO.xlsm");
ExApp.Run("FSO.xlsm!MacroName");
excel_file.Close(); // important
excel_file = null;
ExApp.Quit();
ExApp = null;
}
catch(ex)
{
alert(ex.message);
excel_file.Close(); // important
excel_file = null;
ExApp.Quit();
ExApp = null;
}
}
- 函数参数中的数据与指定变量之间的任何性能差异
- 为什么这在IE中的工作方式与在Firefox中不同
- 与其他库的jQuery.noConflict()
- 将Firebase与Electron结合使用
- 转义符不能与innerHTML一起使用
- 在Windows 10中自动执行例行程序(主要与浏览器交互)
- cron作业与Javascript计时事件
- 如何使用铁流星与流星的默认路线
- 用与线条相同的颜色填充多折线图上的点
- JSON重构(合并内容)与javascript
- 将Angular js与taglib结合使用
- 如何在javascript中获得与特定数字相等的随机数
- 使用元素的值与innerHTML
- 将图像上传ajax与表单提交ajax相结合
- 从客户端获取修改后的对象,并将其与服务器上的原始对象组合
- Google Chrome 问题与 css 和 JScript
- 撇号伊苏与 php 和 jscript
- 如何使通用 Jscript 函数与动态表单一起使用
- 预计& # 39;;& # 39;当尝试运行excel宏与jscript
- JScript数组与自定义数组索引