2014-10-06 30 views
0

使用以下代碼我打開並閱讀excel數據,但文件在UI中打開。如何使用applescript讀取背景中的excel數據

set iRowNo to "A" 
set iCellCount to "2" 
set SheetName to "Sheet1" 
on ExcelFileReadingData(iRowNo, iCellCount, SheetName) 
    set xlsFilePath to (path to desktop as Unicode text) & "SofTestSmokeAutomationSuite:DataSheet.xlsx" as Unicode text 
    tell application "System Events" 
     tell application "Microsoft Excel" 
      tell worksheet SheetName 
       activate 
       open xlsFilePath 
       delay 2 
       set iCellValue to value of range ("" & iRowNo & "" & iCellCount) as text 
      end tell 
     end tell 
     tell application "System Events" 
      tell application process "Microsoft Excel" 
       click button 3 of window "DataSheet.xlsx" 
      end tell 
     end tell 
     return iCellValue 
    end tell 
end ExcelFileReadingData 

我想在後臺打開此文件以及數據應在後臺得到讀應該有與Excel任何UI界面每一件事應該在後臺發生。

Excel版本= 11, O.S MAC = 10.9

回答

0

我沒有嘗試這一點,但這裏是我會怎麼寫你的代碼。

set iRowNo to "A" 
set iCellCount to "2" 
set SheetName to "Sheet1" 

ExcelFileReadingData(iRowNo, iCellCount, SheetName) 

on ExcelFileReadingData(iRowNo, iCellCount, SheetName) 
    -- open the file 
    set xlsFilePath to (path to desktop as text) & "SofTestSmokeAutomationSuite:DataSheet.xlsx" 
    tell application "Microsoft Excel" to open file xlsFilePath 
    delay 2 

    -- hide excel 
    tell application "System Events" 
     set excel to first process whose name is "Microsoft Excel" 
     set visible of excel to false 
    end tell 

    -- get the value 
    tell application "Microsoft Excel" 
     set iCellValue to (value of range (iRowNo & iCellCount) of worksheet SheetName of workbook 1) as text 
    end tell 

    return iCellValue 
end ExcelFileReadingData 
+0

感謝您關注它。但第二次運行腳本時出現錯誤,錯誤如下。 – Girish 2014-10-07 07:09:44

+0

錯誤「Microsoft Excel出現錯誤:AppleEvent處理程序失敗。」數字-10000 – Girish 2014-10-07 07:10:04

+0

我測試了它,並且我沒有多次運行它的錯誤。它按預期工作。你必須弄清楚你的結局發生了什麼。 – regulus6633 2014-10-07 14:11:07

相關問題