Applescript 遍历列,用一定量减去每个单元格的内容,并将结果存储在同一单元格中

Applescript 遍历列,用一定量减去每个单元格的内容,并将结果存储在同一单元格中,applescript,Applescript,我正在尝试打开一个Pages文档。 但是它不允许我打开一个页面文件,这些文件都是灰色的。 之后我想做的是迭代第一个表C列的所有单元格,然后从其内容中减去一定量,当然,将结果保存在同一单元格中。 这是我正在写的,但我不确定,因为我昨天开始学习applescript tell application "Pages" activate try set the chosenDocumentFile to ¬ (choose

我正在尝试打开一个Pages文档。 但是它不允许我打开一个页面文件,这些文件都是灰色的。 之后我想做的是迭代第一个表C列的所有单元格,然后从其内容中减去一定量,当然,将结果保存在同一单元格中。 这是我正在写的,但我不确定,因为我昨天开始学习applescript

  tell application "Pages"
    activate
    try
        set the chosenDocumentFile to ¬
            (choose file of type ¬
                {"com.apple.iwork.pages", ¬
                    "com.microsoft.word.doc", ¬
                    "org.openxmlformats.wordprocessingml.document"} ¬
                    default location (path to documents folder) ¬
                with prompt "Choose the Pages or Microsoft Word document to open:")
        open the chosenDocumentFile
    on error errorMessage number errorNumber
        if errorNumber is not -128 then
            display alert errorNumber message errorMessage
        end if
        
        tell the first table of chosenDocumentFile
            repeat with i from 2 to count of cells of column "C"
                set the value of cell i of column "C" to... #don't know how to subtract an amount and then store the result
            end repeat
        end tell
        
    end try
end tell

在此之前,在一个单独的脚本中,为我正在使用v7的pages文档获取类型idenitifer,不知道它是否有区别。不确定为什么有必要,但获取类型标识符需要两个步骤。本质上,如果字符串中没有sffpages,您也会过滤掉所有页面和文档

tell application "Finder"
    set pgsF to ((path to documents folder) & "sam.pages" as text) as alias
    -- or…
    -- set pgsF to (choose file)
    set infor to info for pgsF
    set ti1 to type identifier of infor
    --> "com.apple.iwork.pages.sffpages"        
end tell
我们可以将其放入新的applescript中,如下所示:

set ti1 to "com.apple.iwork.pages.sffpages"
tell application "Pages"
    activate
    set cdf to choose file of type ti1 with prompt ¬
    "Choose the Pages or Microsoft Word document to open:"
    open cdf
    
    tell page 1 of document 1
        
        set cc to column "C" of table 1
        set cColc to cells 2 thru -1 of cc whose value is not missing value
        set cColv to value of cells 2 thru -1 of cc whose value is not missing value
        
        repeat with fc in cColc
            set value of fc to (value of fc) - 2
        end repeat
        
    end tell
end tell
如果列C的单元格中总是有一个值,那么您可能不需要“which value is not missing value”序列。没有任何错误处理,例如,如果在C列中有非数字数据,将导致错误

您可能需要将表格的“对象放置”设置为“停留在页面上”,然后选择表格,然后查看>显示排列工具>对象放置,或者从检查器的排列选项卡中选择,然后才能在applescript中使用表格。如果未设置,则可能会出现如下错误:Pages出现错误:无法获取文档1第1页的表1。无效索引


更新:现在过滤出一行标题。通过编辑“2”或“-1”,可以排除更多的标题行。

我发现错误,无法将\Cosin\转换为类型编号。数字-1700从余弦到数字。余弦基本上是C列。可能是因为它从列的标题开始,当然不是整数?是的,这可能是罪魁祸首。如果这是一个问题,那么“ccol”行将需要过滤掉标题行。尝试一下。它现在只影响从2开始的那些行。这可以编辑。仅供参考,如果问题中包含代表性数据样本以及此类详细信息,则可能会有所帮助。另一方面,从脚本的角度来看,数字是实数,而不是整数,例如表1的C列的单元格2的值类->实数。我想这就是页面存储它们的方式,但我猜。效果很好!: