想要搭建一个基于JavaScript脚本处理vba代码的网页,已撰写完vba代码,但是不会应用到JavaScript脚本,主要是怎么链接网页
<!DOCTYPE html>
<html>
<head>
<title>网页标题</title>
</head>
<body>
<input type="file" id="fileInput" />
<script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script>
<script>
// 监听文件上传事件
document.getElementById('fileInput').addEventListener('change', function() {
// 获取上传的文件
var file = this.files[0];
// 创建FileReader对象,用于读取文件内容
var reader = new FileReader();
// 监听文件读取完成事件
reader.addEventListener('load', function() {
// 获取读取的Excel文件内容
var excelData = reader.result;
// 使用js-xlsx库解析Excel文件并获取数据
var workbook = XLSX.read(excelData, { type: 'binary' });
var worksheet = workbook.Sheets[workbook.SheetNames[0]];
var data = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
// 调用VBA代码处理Excel表格数据
var vbaCode = 'Sub 未熄灯扣分系统()\n' +
'Dim ws1 As Worksheet\n' +
'Dim lastRow1 As Long\n' +
'Dim i As Long, j As Long\n' +
'Dim dorm As String, id As String, name As String\n' +
'Dim count As Integer, commentText As String\n' +
'Dim dormNumbers() As String, dormNumber As String, dateStr As String\n' +
'On Error Resume Next\n' +
'Set ws1 = ThisWorkbook.Worksheets("Sheet1")\n' +
'lastRow1 = ws1.Cells(ws1.Rows.count, "A").End(xlUp).Row\n' +
'count = 0\n' +
'dormNumber = prompt("请输入要查找的宿舍号,多个宿舍号请用逗号分隔:", "未熄灯扣分系统(艺术设计学院自律会办公室制)")\n' +
'dateStr = prompt("请输入日期(格式:2023.01.15):", "未熄灯扣分系统(艺术设计学院自律会办公室制)")\n' +
'dormNumbers = Split(dormNumber, ",")\n' +
'For j = 0 To UBound(dormNumbers)\n' +
' dormNumber = Trim(dormNumbers(j))\n' +
' For i = 2 To lastRow1\n' +
' dorm = ws1.Cells(i, "A").Value\n' +
' id = ws1.Cells(i, "B").Value\n' +
' name = ws1.Cells(i, "C").Value\n' +
' If dorm = dormNumber And name <> "" Then\n' +
' If ws1.Cells(i, "C").Font.Color = vbRed Then\n' +
' If ws1.Cells(i, "O").Value = "" Then\n' +
' ws1.Cells(i, "O").Value = 0\n' +
' End If\n' +
' ws1.Cells(i, "O").Value = ws1.Cells(i, "O").Value - 1\n' +
' commentText = dateStr & "未熄灯-1"\n' +
' If ws1.Cells(i, "O").Comment Is Nothing Then\n' +
' Set Comment = ws1.Cells(i, "O").AddComment(commentText)\n' +
' Else\n' +
' Set Comment = ws1.Cells(i, "O").Comment\n' +
' Comment.Text Comment.Text & vbNewLine & commentText\n' +
' End If\n' +
' count = count + 1\n' +
' ElseIf ws1.Cells(i, "C").Font.Color = vbBlack Then\n' +
' If ws1.Cells(i, "O").Value = "" Then\n' +
' ws1.Cells(i, "O").Value = 0\n' +
' End If\n' +
' 'ws1.Cells(i, "O").Value = ws1.Cells(i, "O").Value - 0.5\n' +
' commentText = dateStr & "未熄灯-0.5"\n' +
' If ws1.Cells(i, "O").Comment Is Nothing Then\n' +
' Set Comment = ws1.Cells(i, "O").AddComment(commentText)\n' +
' Else\n' +
' Set Comment = ws1.Cells(i, "O").Comment\n' +
' tempStr = Comment.Text\n' +
' If InStr(tempStr, dateStr) = 0 Then\n' +
' Comment.Text Comment.Text & vbNewLine & commentText\n' +
' End If\n' +
' End If\n' +
' count = count + 1\n' +
' End If\n' +
' End If\n' +
' Next i\n' +
'Next j\n' +
'Dim ws2 As Worksheet\n' +
'Dim rng As Range\n' +
'Dim cell As Range\n' +
'Dim count2 As Long\n' +
'Set ws2 = ThisWorkbook.Worksheets("Sheet2")\n' +
'Set rng = ws2.Range("A2:A" & ws2.Cells(ws2.Rows.count, "A").End(xlUp).Row)\n' +
'count2 = 0\n' +
'For Each cell In rng\n' +
' i = cell.Row\n' +
' If ws2.Cells(i, "D").Value = "No" Then\n' +
' commentText = "Need Improvement"\n' +
' ElseIf ws2.Cells(i, "D").Value = "Yes" Then\n' +
' commentText = "Good Job"\n' +
' Else\n' +
' commentText = ""\n' +
' End If\n' +
' If ws2.Cells(i, "O").Comment Is Nothing Then\n' +
' Set Comment = ws2.Cells(i, "O").AddComment(commentText)\n' +
' count2 = count2 + 1\n' +
' Else\n' +
' Set Comment = ws2.Cells(i, "O").Comment\n' +
' tempStr = Comment.Text\n' +
' If InStr(tempStr, commentText) = 0 Then\n' +
' Comment.Text Comment.Text & vbNewLine & commentText\n' +
' End If\n' +
' End If\n' +
'Next cell\n' +
'resultMsg = "第一部分:" & vbCrLf & "查找到" & count & "条数据,添加数据" & count & "条,添加批注" & count & "条" & vbCrLf & vbCrLf\n' +
'If count2 > 0 Then\n' +
' resultMsg = resultMsg & "第二部分:" & vbCrLf & "共添加了" & count2 & "条批注"\n' +
'Else\n' +
' resultMsg = resultMsg & "第二部分:" & vbCrLf & "无数据需要添加批注"\n' +
'End If\n' +
'MsgBox resultMsg\n' +
'End Sub';
try {
// 创建ActiveXObject实例
var excelApp = new ActiveXObject("Excel.Application");
// 打开Excel文件
var workbook = excelApp.Workbooks.Open(file.name);
// 执行VBA代码
excelApp.Run(vbaCode);
// 关闭Excel文件并退出Excel应用程序
workbook.Close(false);
excelApp.Quit();
} catch (e) {
alert("无法执行VBA代码,请{"message":"出错了","data":null,"status":"Fail"}
这个是已经写好的JavaScript脚本和vab结合的代码,就是不会应用