我正在寻找解决这个问题的方法。
我有大量 .csv 文件,我正在寻找一种将它们导入 Excel 的方法,作为单个工作簿中的单独工作表
现在我在另外两个论坛上问过了,在一些帮助后留下了两个未完成的代码。
首先是VBA
Sub test()
Dim MacReturn As String
Dim FilePaths As Variant
MacReturn = MacScript(ScriptString)
FilePaths = Split(MacReturn, ",")
If UBound(FilePaths) = -1 Then
MsgBox "chosen folder had no CSV files"
Else
If FilePaths(0) = "false" Then
MsgBox "cancel button pressed"
Else
MsgBox (UBound(FilePaths) + 1) & " CSV files in folder."
End If
End If
End Sub
Function ScriptString() As String
ScriptString = "tell application ""Finder"""
ScriptString = ScriptString & vbCr & " try"
ScriptString = ScriptString & vbCr & " set CSVfiles to (files of (choose folder) whose name extension = ""csv"")"
ScriptString = ScriptString & vbCr & " set allPaths to {}"
ScriptString = ScriptString & vbCr & " repeat with oneFile in CSVfiles"
ScriptString = ScriptString & vbCr & " set OnePath to """""
ScriptString = ScriptString & vbCr & " repeat until (name of oneFile = """")"
ScriptString = ScriptString & vbCr & " set OnePath to (get name of (oneFile)) & "":"" & OnePath"
ScriptString = ScriptString & vbCr & " set oneFile to (container of oneFile)"
ScriptString = ScriptString & vbCr & " end repeat"
ScriptString = ScriptString & vbCr & " copy (text 1 thru -2 of OnePath) to end of allPaths"
ScriptString = ScriptString & vbCr & " end repeat"
ScriptString = ScriptString & vbCr & "activate application ""Microsoft Excel"""
ScriptString = ScriptString & vbCr & " return allPaths"
ScriptString = ScriptString & vbCr & " on error"
ScriptString = ScriptString & vbCr & "activate application ""Microsoft Excel"""
ScriptString = ScriptString & vbCr & " return false"
ScriptString = ScriptString & vbCr & " end try"
ScriptString = ScriptString & vbCr & "end tell"
End Function
VBA 似乎可以工作,并在您点击 OK 时要求您选择一个文件夹,什么都没有导入???
第二个都是applescript
tell application "System Events"
set CSVfiles to path of (files of (choose folder) whose name extension = "csv")
end tell
tell application "Microsoft Excel"
activate
open workbook workbook file name "HD:Users:<my name>:Documents:Office:SOLAR:converted:Daily solar Generation.xlsx"
set targetSheet to worksheet 1 of workbook (name of active workbook)
repeat with thisFile in CSVfiles
open text file filename thisFile data type delimited
tell active workbook
open workbook workbook file name "HD:Users:<myname>:Documents:Office:SOLAR:converted:Daily solar Generation .xlsx"
set sourceBookName to its name
set sourceSheet to sheet 1
end tell
copy worksheet sourceSheet after targetSheet
close workbook sourceBookName
end repeat
end tell
这个似乎挂起并超时并显示错误代码
错误“系统事件出错:AppleEvent 超时。” 号码-1712
我已经在这里待了将近一个星期,虽然它很有趣。(我特别喜欢了解applescript),真的可以用一只手试图找出其中一个或两个有什么问题!