AppleScript - 如果文件在Excel工作表中列出,请添加另一个列值作为前缀

时间:2017-01-26 05:41:34

标签: excel applescript rename automator applescript-excel

事实:

  • 我有一个包含一堆.pdf文件的文件夹。名字看起来像(2-581.pdf,131-681.pdf,1138-4.pdf,...)
  • .pdf文件分为两部分partA-partB.pdf分隔为" - "
  • 我有一张excel表,列出了文件夹
  • 中的所有结果
  • Excel工作表中列出了许多pdf,比文件夹中的更多。
  • Excel工作表前两行是标题和标题
  • Excel工作表第一列是测试#,它将是前缀
  • Excel工作表第4列是原始.pdf文件名的第A部分,第5列第B部分

理想的结果 - 我运行automator / applescript并将其指向包含.pdfs的文件夹和包含该信息的excel表。它运行并重命名excel中找到的文件夹中的所有.pdf文件。重命名的文件将包含test#,空格和原始名称。 excel表中的行将以某种方式突出显示/标记,显示已找到它。

我搜索了几个不同的论坛以及这个论坛,并尝试拼凑代码以尝试获得一些工作。我不会粘贴此代码,因为我还没有在我的方案中使用它。

我合并的以下代码从另一个帮助论坛复制并尝试根据我的方案进行调整。它有点适用于前6个项目然后停止(我认为因为第7项不在文件夹中)。此外,为了使其工作,我不得不在excel中手动创建原始文件名和新文件名。我这样做是因为我无法让它在excel之外工作。

欢迎任何和所有帮助,建议,可能解决方案的链接。我有很多测试结果可以在许多不同的excel表上进行,这将节省我很多时间。我今天想失去自己。我要么在思考这个问题,要么在我的头脑中。谢谢!

set KeyFileName to choose file with prompt "Choose the Excel file"
set WorkingDirectory to (choose folder with prompt "Please locate the folder  with the processed .pdf's.") as string
set KeyFilePath to WorkingDirectory & KeyFileName
set FileNamesKey to my GetFileNamesKey(KeyFilePath)
set theResult to ChangeFileNames(WorkingDirectory, FileNamesKey)
return theResult
end run

to GetFileNamesKey(KeyFilePath)
    tell application "Microsoft Excel"

    set ResultsRange to used range of active sheet of active workbook

    set FileNames to {}
    repeat with i from 1 to count of rows in ResultsRange
        set NewFileName to value of third column of row i of ResultsRange
        set OldFileName to value of second column of row i of ResultsRange
        set FileNames to FileNames & {{OldFileName, NewFileName}}

    end repeat

    return FileNames

end tell
end GetFileNamesKey

to ChangeFileNames(WorkingDirectory, FileNamesKey)

tell application "Finder"
    repeat with aFile in FileNamesKey
        set OldFileName to item 1 of aFile
        set NewFileName to item 2 of aFile



        set PathToFile to WorkingDirectory & OldFileName
        try
            set FileExtension to name extension of file (WorkingDirectory & (item 1 of aFile))
            set name of file PathToFile to NewFileName & "." & FileExtension
        on error
            exit repeat
            return false
        end try
    end repeat
end tell

return true
end ChangeFileNames
return input
end run

1 个答案:

答案 0 :(得分:0)

如果我很清楚,你有一个excel文件,其中col.A = #test,col.B = oldFileName(xx-yyy.pdf),col.C = NewName(空?),Col.D = PartA( xx),Col.E = PartB(yyy)

您希望循环浏览该文件的所有行,搜索Col.B中的文件是否存在于所选文件夹中,如果存在,则需要更改文件的名称(= colA +"&# 34; +旧名称)并在Excel行中显示。

如果没问题,那么下面的脚本会做你想要的。如果在所选文件夹中找到文件,我用2种方式标记了Excel行:在C列中设置新文件名(不确定它之前是否为空?)并将其颜色设置为红色。我添加了很多评论以表明它(我希望!):

set KeyFileName to choose file with prompt "Choose the Excel file"
set WorkingDirectory to (choose folder with prompt "Please locate the folder  with the processed .pdf's.") as string

tell application "Microsoft Excel"
open KeyFileName
set ResultsRange to used range of active sheet of active workbook
-- Data bloc starts row 2 with headers and then row 3 to x
-- col.A= #test, col.B= oldFileName (xx-yyy.pdf), col.C= NewName (empty), Col.D= PartA (xx), Col.E= PartB (yyy)
repeat with i from 2 to count of rows in ResultsRange
    -- loop through each Excel row only starting row 2 of the range to skip hearders
    set OldFileName to value of second column of row i of ResultsRange
    set NewName to (value of the first column of row i of ResultsRange) & " " & OldFileName
    set theFile to WorkingDirectory & OldFileName
    tell application "Finder" to set FileExist to (file theFile exists) --check file exists
    if FileExist then
        tell application "Finder" to set name of file theFile to NewName --change file name in the folder
        set (value of third column of row i of ResultsRange) to NewName -- set new name in Excel col.C
        set (color index of interior object of third column of row i of ResultsRange) to 3 -- = Red !
    end if
end repeat --- next Excel row
end tell

该行"打开KeyFileName"可以跳过的是Excel文件已经打开。此外,脚本不会在更改后保存Excel文件!