如何使用applescript在后台读取excel数据

时间:2014-10-06 13:32:01

标签: excel applescript

使用以下代码我打开并阅读excel数据,但文件在UI中打开。

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

1 个答案:

答案 0 :(得分: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