Xlwings库——自动化Excel基石

这个库用法与Python完全一致,随心更新。

; Author: Mono
; Time: 2022.08.09
; Version: 0.0.1

; **注意所有文件路径均为完整路径,若为不完整路径
; **则优先访问我的文档

Class Xlwings
{
    Class App
    {
        __New(visible := True, add_book := False)
        {
            this.app := ComObject("Excel.Application") ; handle
            this.app.Visible := visible ; by default excel sheets are invisible
            
            if add_book
                this.book := this.app.Workbooks.Add()
            
            this.books := Xlwings.app.book(this.app)
            this.DefineProp("display_alerts", {Get : DAGet, Set : DASet})
            
            DAGet(_)
            {
                Return this.app.DisplayAlerts
            }
            
            DASet(_, Value)
            {
                this.app.DisplayAlerts := Value
            }
        }
        
        Quit()
        {
            this.app.quit()
        }
        
        Range(item)
        {
            Return this.app.Range(item)
        }
        
        Class Book
        {
            __New(app)
            {
                this.app := app
            }
            
            Close(Sign := True, Url := "")
            {
                if !Url
                    this.wb.Close(Sign)
                
                else
                {
                    Try
                        this.wb.Close(Sign, Url)
                    
                    Catch
                        this.wb.Close(Sign, A_ScriptDir "\" Url)
                }
            }
        
            Open(Url)
            {
                Try
                    this.wb := this.app.Workbooks.open(Url)
                
                Catch
                    this.wb := this.app.Workbooks.open(A_ScriptDir "\" Url)
                
                this.Name := this.wb.Name
                this.FullName := this.wb.FullName
                this.sheets := Xlwings.app.book.sheet(this.wb)
                Return this
            }
            
            Save(Url := "")
            {
                if !Url
                    Return this.wb.save()
                
                else
                    Return this.Saveas(Url)
            }
            
            Saveas(Url)
            {
                Try
                    Return this.wb.saveas(Url)
                
                Catch
                    Return this.wb.saveas(A_ScriptDir "\" Url)
            }
            
            Class Sheet
            {
                __New(wb)
                {
                    this.wb := wb
                }
                
                __Item[Index]
                {
                    Get => this.GetFunc(Index)
                    Set => ""
                }
                
                GetFunc(Index)
                {
                    this.sheet := this.wb.sheets[Index]
                    Return this
                }
                
                Range(Index)
                {
                    Return this.sheet.Range(Index)
                }
            }
        }
    }
}
; 测试
app := xlwings.app()
wb := app.books.open("C:\Users\HP\Desktop\MST优化时代.xlsx")
; ws := wb.sheets[1]
ws := wb.sheets["Sheet1"]
app.display_alerts := 1
; ws.range("A4").value := 5
ws.range("A4:A7")[0].value := 5
; msgbox ws.Range("A4:A7")[2].Value
wb.save()
wb.close()
app.quit()

给TA捐赠
共{{data.count}}人
人已捐赠
AHKV2教程案例

分享一个纯AHK的MD5加密

2022-8-9 18:20:54

AHKV2函数办公

调用在线微软数学计算(正在完善)

2022-8-10 14:25:15

4 条回复 A文章作者 M管理员
  1. zhyb

    这个需要安装excel

  2. cnkiller

    牛逼plus啊

  3. 11010010

    👍

个人中心
有新私信 私信列表
搜索