我使用vbscript通过命令行在Excel中复制和粘贴数据。我想在Mac上执行相同的工作。以下是我用于Windows的vbscript:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set srcData = objExcel.Workbooks.Open("sourcefile.xlsx")
Set destinationData = objExcel.Workbooks.Open("destinationfile.xlsm")
srcData.sheets("Sheet1").Range("A1").copy destinationData.sheets("Sheet1").Range("A1")
srcData.close False
我不知道如何在Mac中执行此操作。请帮我转换脚本。
答案 0 :(得分:0)
我相信你唯一需要改变的不是" sourcefile.xlsx"和" destinationfile.xlsm"你需要添加完整的路径。请注意,在mac上,路径的编写方式与在Windows上的编写方式不同。
Windows EX:C:\ Documents \ sourcefile.xlsx
Mac EX:OSX:用户:"用户名":文档:sourcefile.xlsx
在Mac上,您可以使用以下代码获取用户名
Function GetUserName() As String
Dim sMyScript As String
sMyScript = "set userName to short user name of (system info)" & vbNewLine & "return userName"
GetUserName = MacScript(sMyScript)
End Function
在您的代码中,这看起来像这样:
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set srcData = objExcel.Workbooks.Open("OSX:Users:" & GetUserName.GetUserName & ":Documents:sourcefile.xlsx")
Set destinationData = objExcel.Workbooks.Open("OSX:Users:" & GetUserName.GetUserName & ":Documents:destinationfile.xlsm")
你也可以:
Dim xlPath As String
xlPath = "OSX:Users:" & GetUserName.GetUserName & ":Documents:"
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set srcData = objExcel.Workbooks.Open(xlPath & "sourcefile.xlsx")
Set destinationData = objExcel.Workbooks.Open(xlPath & "destinationfile.xlsm")
对我来说,这工作得很好。希望它也适合你。
CU