我的任务:打开.csv文件的vbscript将数据读入数组,使用该数据执行函数,然后将更新的数据返回到同一文件。我相信除了我的函数之外我还有其他一切工作来读取文件中的数据。
我的问题:我在尝试使用此代码时收到运行时错误。使用脚本原样我得到一个错误:类型不匹配。我尝试改变代码并获得不同的运行时错误。
关于此代码的一些注释:
我的代码来读取文件:
Function BuildArrayFromCsv(filepath) 'Function to read file and load into an array
Const ForReading = 1 ' Declare constant for reading for more clarity
Set inputFile = FileSysObj.OpenTextFile(filepath, ForReading, True) ' Set inputFile as file to be read from
Dim row, column
Dim fields(11) '12 fields per line
inputFile.ReadAll 'read to end of file
ReDim MyArray(11,inputFile.Line-2) 'current line, minus one for header, and minus one for starting at zero
inputFile.close 'close file so that MyArray can be filled with data starting at the top
Set inputFile = FileSysObj.OpenTextFile(filepath, ForReading, True) 'back at top
inputFile.ReadLine 'skip header
Do Until inputFile.AtEndOfStream
fields = Split(inputFile.Readline,",") 'store line in temp array
For column = 0 To 11 'iterate through the fields of the temp array
myArray(row,column) = fields(column) 'store each field in the 2D array with the given coordinates
Next
row = row + 1 'next line
Loop
inputFile.close
End Function
答案 0 :(得分:3)
在VBScript Dim name(n)
中创建一个大小为n + 1的固定数组;这样的野兽不能用动态覆盖/覆盖(如Split()返回)。证据
>> Dim fields(1)
>> fields = Split("a b")
>>
Error Number: 13
Error Description: Type mismatch
>> Dim fields
>> fields = Split("a b")
>> WScript.Echo Join(fields)
>>
a b
>>
所以替换
Dim fields(11) '12 fields per line
与
Dim fields 'should be 12 fields per line, checking the result of Split() advisable