根据Numbers表中的列表在AppleScript中设置变量

时间:2017-03-28 15:14:59

标签: applescript applescript-numbers

AppleScript初学者。搜索高低并没有让我得到答案。

我正在使用AppleScript来帮助举办青少年摔跤锦标赛。每个部门(根据年龄)分为重量级别。例如:新手80或Cadet 105。

一旦某一组孩子进入某个分组/体重级别,这些孩子就会被添加到包含其支架的新表格中(想想March Madness支架,但少数孩子在摔跤而不是打篮球)。 / p>

我已经想出如何将一个组放到一个新的工作表中,在这个工作表中填充括号,但是当创建这个新工作表时,我不知道如何使AppleScript将工作表的名称更改为正确的部门/重量级。我确定它与基于分区/权重类列表(我有)创建变量有关,但我无法弄清楚如何做到这一点。这是代码的相关部分:

tell document 1
        set active sheet to the last sheet
        set thisSheet to make new sheet
        set the name of thisSheet to "[Division variable – Weight class variable]"
        tell thisSheet
            delete every table
        end tell

关于如何使AppleScript像我想要的那样命名工作表的任何想法?

4 个答案:

答案 0 :(得分:0)

尝试

set thisSheet to make new sheet with properties  {name:"yourname"}

修改 一些解释:如果您在如何使用第三方应用程序时苦苦挣扎,请尝试加载它的库并查找您需要的方法。在applescript编辑器中,你会在(我猜)菜单中找到它。然后选择所需的应用程序以获取库

答案 1 :(得分:0)

为了给你一个小例子,你可以想象你正在追求的是什么,这是一个小片段。我认为它是自我解释的,遍历标题列表,然后将名称应用于表格。

set divisionNames to {"Novice", "Cadet"} -- How You Grab These Values Matters
set weightClasses to {"80", "105"} -- These Values Too

tell application "Numbers"
    activate
set thisDocument to make new document
tell thisDocument
    repeat with i from 1 to count of divisionNames
        make new sheet with properties {name:item i of divisionNames & space & item i of weightClasses}
    end repeat
end tell
end tell

或者,如果你从整个列表中提取值,那么你可以

set sheetTitles to {"Novice 80", "Cadet 105"}

tell application "Numbers"
activate
set thisDocument to make new document
tell thisDocument
    repeat with division in sheetTitles
        make new sheet with properties {name:division}
    end repeat
end tell
end tell

编辑:本着帮助低预算或无预算的学校/组织的精神......这是回答评论中发表的第二个问题的另一个例子。在不知道数据结构的情况下,很难给出您对特定案例的确切答案。此外,这里还有一个指向网站的链接,可以帮助您进一步推进项目进展。 https://iworkautomation.com/numbers/index.html

(*
set sampleKids to {"John Doe", "Jane Doe", "Johnny Foe", "Janie Foe", "Tommy Joe", "Tammy Joe"}
set sampleDivisions to {"Novice-80", "Novice-85", "Cadet-105", "Cadet-110", "Novice-80", "Cadet-105"}

tell application "Numbers"
activate
set thisDoc to make new document with properties {name:"Wrestling Sample"}
tell thisDoc
    set the name of sheet 1 to "Sign In Sheet"
    tell active sheet
        delete every table
        set newTable to make new table with properties {row count:(count of sampleKids) + 1, column count:2, name:"Sign In Sheet"}
        tell newTable
            set value of cell 1 of column "A" to "Name"
            set value of cell 1 of column "B" to "Division"
            set x to 2
            repeat with eachName in sampleKids
                set value of cell x of column "A" to eachName
                set x to (x + 1)
            end repeat
            set x to 2
            repeat with eachDivision in sampleDivisions
                set value of cell x of column "B" to eachDivision
                set x to (x + 1)
            end repeat
        end tell
    end tell
end tell
end tell
*)

--********** IGNORE ABOVE THIS LINE IT'S ONLY BUILDING A SAMPLE TABLE **********--
--********** SAVE ABOVE TO ANOTHER SCRIPT FOR TESTING WITH NEW TABLE **********--

(*
ERROR HANDLING ISN'T PRESENT - AN EMPTY CELL IN FIRST COLUMN OR CHOSEN COLUMN WILL THROW AN 
ERROR SINCE THEY ARE THE IMPORTANT PIECES OF DATA FOR GRABBING LISTS - SAVE SCRIPT
TO NUMBERS SCRIPT FOLDER OF YOUR CHOICE - ENTER ALL INFO ON FIRST TABLE OF FIRST SHEET OF 
DOCUMENT THEN RUN SCRIPT - SCRIPT ACCEPTS ANY AMOUNT OF ROWS OR COLUMNS
*)

tell application "Numbers"
activate
-- Display A Simple Reminder That You're About To Lose Some Existing Data
display dialog "This Script Will Delete All Sheets Except The First Sheet Of This Document Before It Proceeds To Make New Sheets & Tables Based On The First Table Of The First Sheet." buttons {"Cancel", "Start"} default button 2 with icon 1
tell document 1
    -- Get A List of the Sheet Names
    set sheetNames to name of sheets
    -- Start With A Fresh Slate, No Old Sheets
    delete (every sheet whose name is not item 1 of sheetNames)
    tell sheet 1
        -- Grab and Set Future Header Values
        set columnHeaders to value of cell of row 1 in table 1
        -- Display A List Of Possible Choices To Create New Sheets With From The List We Make Above
        set chosenColumn to choose from list columnHeaders with prompt "Which Column Do You Want To Use For New Sheets?" default items item 1 of columnHeaders
        set chosenColumn to chosenColumn as text
        tell table 1
            -- Remove All Empty Rows to Help Prevent Error In Script
            set {row_count, col_count} to {count rows, count columns}
            set blank_row to {}
            repeat with x from 1 to col_count
                set blank_row to blank_row & missing value
            end repeat
            set x to 1
            -- Delete Empty Rows In Reverse, It's Logical
            repeat with y from row_count to 1 by -1
                set row_values to value of cells of row y
                if row_values = blank_row then delete row y
            end repeat

            -- Grab A List of All Divisions for Future Use Depending on Choice From Prompt, excluding the First Row Which Is A Header. If You Selected The First Column, We Have to Handle That Differently
            if chosenColumn is item 1 of columnHeaders then
                set theDivisions to the value of every cell of column "A" whose value is not chosenColumn
            else
                set theDivisions to the value of every cell of column named chosenColumn whose value is not chosenColumn
            end if

        end tell
    end tell
    -- Start the New "Sheet Making" Loop
    repeat with division in theDivisions
        -- Make An Empty Blank List At the Start of Every Loop
        set matchingDivisions to {}
        tell table 1 of sheet 1

            -- Get All Rows Matching the Current Division of the Loop We Are On, to Make New Tables With Later
            repeat with x from 1 to count of cells
                if the value of cell x is division then
                    -- Put All Data About the Rows We Gathered Above Into the Empty List We Made 
                    set the end of matchingDivisions to value of cells of row of cell x
                end if
            end repeat
            -- Reset x, Because I'm Not Creative and Want to Use It Later
            set x to 1
        end tell
        -- If The Sheet Of the Division We Are On, of the Loop, Doesn't Exist, Make It
        if not (exists sheet division) then
            make new sheet with properties {name:division}
            tell sheet division
                -- Start With A Fresh Slate On This New Sheet
                delete every table
                -- Make the Table With All Relevant Parameters 
                set currentDivisionTable to make new table with properties ¬
                    {row count:((count of matchingDivisions) + 1), column count:count of item 1 of matchingDivisions, name:division}
                tell currentDivisionTable
                    set x to 1
                    -- Set The Header Values from A List We Created Earlier
                    repeat with theHeader in columnHeaders
                        set the value of cell x to theHeader
                        set x to (x + 1)
                    end repeat
                    -- Reset x Again, I'm Lazy
                    set x to 1
                    -- Set Starting Point to Start Filling The Table, Compensate For Our Headers
                    set rowIndex to 1
                    set columnIndex to 0
                    -- Start Filling The Table With Data, Which Comes From The List Earlier
                    repeat with x from 1 to count of the matchingDivisions
                        set rowData to item x of the matchingDivisions
                        tell row (rowIndex + x)
                            repeat with i from 1 to the count of rowData
                                tell cell (columnIndex + i)
                                    set value to item i of rowData
                                end tell
                            end repeat
                        end tell
                    end repeat
                end tell
            end tell
        end if
    end repeat
    -- Return To the First Sheet
    set the active sheet to the first sheet
    -- Display Notification That The Tables Are Done Being Made -- OPTIONAL
    display notification "Processing is complete." with title "Numbers Table Converter" subtitle "All Tables Have Been Made." sound name "Hero"
end tell
end tell

答案 2 :(得分:0)

完整代码修改如下:

        set {begCol, endCol} to {2, 17}
        set tgtCol to 1

        tell application "Numbers"
            tell front sheet of front document
                tell active sheet
                    set getVal to rows's cells's value
                    set myOriginalTable to front table
                    set itemCode to {"CardNo", "SAL-DAY", "OT15-Money", 
    "OT2-Money", "OT3-Money", "OTHINC", "P0007", "DILIG", "P0004", "P0003", 
    "SEV_SPE", "P0011", "SI-SSF", "TI-TAXITEM", "P0022", "P0021", "P0025"} -- 
    change to variable 
                    set Amount to {"CardNo", "SAL-DAY", "OT15-Money", 
    "OT2-Money", "OT3-Money", "OTHINC", "P0007", "DILIG", "P0004", "P0003", 
    "SEV_SPE", "P0011", "SI-SSF", "TI-TAXITEM", "P0022", "P0021", "P0025"} -- 
    change to variable 
                    set setCols to 8
                end tell
                set myNewTable to make new table with properties ¬
                    {column count:setCols, row count:(count of itemCode) + 1, 
header column count:0}
                tell myNewTable
                    set value of cell 1 of column "A" to "cardNo"
                    set value of cell 1 of column "B" to "emCode"
                    set value of cell 1 of column "C" to "emName"
                    set value of cell 1 of column "D" to "itemCode"
                    set value of cell 1 of column "E" to "itemName"
                    set value of cell 1 of column "F" to "effDate"
                    set value of cell 1 of column "G" to "amt"
                    set value of cell 1 of column "H" to "remark"
                    set x to 2
                    repeat with eachAmount in Amount
                        set value of cell x of column "G" to eachAmount
                        set x to (x + 1)
                    end repeat
                    set x to 2
                    repeat with eachItemCode in itemCode
                        set value of cell x of column "D" to eachItemCode
                        set x to (x + 1)
                    end repeat
                end tell
            end tell
        end tell

谢谢

答案 3 :(得分:0)

根据上面的源代码。我找到了使用范围选择的其他方法来确定数据。但它仍然不正确,因为代码将所有数据提取到这样的一个单元格。 result after ran script

有人可以建议我吗?

try
    tell application "Numbers" to tell front document to tell active sheet
        set delimiter to ","
        set selected_table to first table whose class of selection range is range
        tell selected_table
            set my_selection to the selection range
            set begCol to address of first column of my_selection
            set endCol to address of last column of my_selection
            set begRow to address of first row of my_selection
            set endRow to address of last row of my_selection
            set getVal to ""
            repeat with j from begRow to endRow
                repeat with i from begCol to endCol
                    set getVal to (getVal & (value of cell j of column i of selected_table) as text) & delimiter

                    set getVal to getVal & return
                end repeat
            end repeat
        end tell
    end tell

    set AmountVal to {getVal}

    tell application "Numbers"
        activate
        tell front sheet of front document
            set myOriginalTable to front table
            set setCols to 8
            set myNewTable to make new table with properties ¬
                {row count:(count of AmountVal) + 1, column count:setCols, header column count:0}
            tell myNewTable
                set value of cell 1 of column "A" to "cardNo"
                set value of cell 1 of column "B" to "emCode"
                set value of cell 1 of column "C" to "emName"
                set value of cell 1 of column "D" to "itemCode"
                set value of cell 1 of column "E" to "itemName"
                set value of cell 1 of column "F" to "effDate"
                set value of cell 1 of column "G" to "amt"
                set value of cell 1 of column "H" to "remark"

                set x to 2
                repeat with eachAmount in AmountVal
                    set value of cell x of column "G" to eachAmount
                    set x to (x + 1)
                end repeat
            end tell
        end tell
    end tell

    display notification "Already Done!" with title "Numbers"
on error
    display dialog "Select a range first and then try again"
end try