如何将数字或Excel文件转换为数据库以进行AppleScript查询?
最终,我希望能够做到以下几点:如何将数字或Excel文件转换为数据库以进行AppleScript查询?,excel,numbers,applescript,Excel,Numbers,Applescript,最终,我希望能够做到以下几点: 维护条目记录,包括:ID#、名字、姓氏、, 电子邮件地址 在Hazel中设置规则,在数据库中搜索文档中的名字或姓氏,并使用相应的ID#重命名文档 理想情况下,这将在后台运行而不打开任何文件,因此使用“数据库事件”的AppleScript查询似乎是最佳选择。不过,我没有任何数据库创建或编辑软件(除非我不知道OS X中有一个默认选项),所以我想最好将我本来创建的Excel/Numbers文件转换为数据库,然后将该数据库用于脚本 我正在测试以下脚本,以将数字表转换为sq
谢谢 注意,数据库事件只是一个哑键值对象存储,而不是关系数据库。它碰巧使用sqlite作为其后台存储,但您不能对其执行SQL操作,只能执行标准的Apple事件查询。(iworkautomation/macosxautomation的作者并不是最聪明的灯泡。)但如果你只是通过名字查找,那可能就足够了。剧本本身就是纯意大利面;如果您想尝试调试它,那么首先删除顶部的
try
行和底部的on error…结束try
行,然后再次运行它,这样您就可以看到错误实际发生的位置。FWIW,如果您了解Python或Ruby之类的知识,那么您应该能够找到库来直接读取xls/xlsx文件。如果您的电子表格文件定期更改,这可能比每次更改时都必须手动重新导出它更可取(这很容易忘记)。e、 g.我用于读取xslx文件;撇开奇怪的恼怒不谈(有什么新的吗?),它可以完成任务,除非您的电子表格非常庞大,否则它应该足够快,可以在Python中迭代查找您想要的行。
property extractDataWithFormatting : false
property coerceNumberToInteger : true
property documentsFolder : missing value
set documentsFolder to (path to documents folder)
tell application "Numbers"
activate
try
if not (exists document 1) then error number 1000
tell document 1
try
tell active sheet
set the selectedTable to ¬
(the first table whose class of selection range is range)
end tell
on error
error number 1001
end try
display dialog "This script will create an SQLite database with the data contained in the selected table." buttons {"Cancel", "Begin"} default button 2 with icon 1
tell selectedTable
set databaseName to the name of it
tell application "Finder"
if not (exists folder "Databases" of documentsFolder) then
make new folder at documentsFolder with properties {name:"Databases"}
end if
set thisName to databaseName
set loopCounter to 1
repeat
if exists document file (thisName & ".dbev") of folder "Databases" of documentsFolder then
set thisName to databaseName & "-" & (loopCounter as string)
set loopCounter to loopCounter + 1
else
set databaseName to thisName
exit repeat
end if
end repeat
end tell
set rowCount to (the count of rows)
set headerRowCount to header row count
set headerColumnCount to header column count
if headerRowCount is not 1 then error number 1002
set the fieldNames to the value of cells (headerColumnCount + 1) thru -1 of row 1
tell application "Database Events"
close every database
set targetDatabase to ¬
make new database with properties {name:databaseName}
end tell
repeat with i from (1 + headerRowCount) to rowCount
if extractDataWithFormatting is true then
set thisRowData to the formatted value of every cell of row i
else
set thisRowData to the value of every cell of row i
end if
if coerceNumberToInteger is true then
repeat with w from 1 to the count of thisRowData
set thisDataItem to item w of thisRowData
if the class of thisDataItem is in {real, number} then
set item w of thisRowData to (thisDataItem as integer)
end if
end repeat
end if
tell application "Database Events"
tell targetDatabase
set thisRecord to make new record with properties {name:""}
tell thisRecord
repeat with q from 1 to the count of fieldNames
set thisFieldName to item q of fieldNames
set thisFieldData to item q of thisRowData
make new field with properties ¬
{name:thisFieldName, value:thisFieldData}
end repeat
end tell
end tell
end tell
end repeat
end tell
end tell
tell application "Database Events"
tell targetDatabase
save
end tell
quit
end tell
display dialog "The database has been created." buttons {"Reveal", "Done"} default button 2 with icon 1
if the button returned of the result is "Reveal" then
tell application "Finder"
activate
open folder "Databases" of documentsFolder
end tell
end if
on error errorMessage number errorNumber
if errorNumber is 1000 then
set alertString to "MISSING RESOURCE"
set errorMessage to "Please create or open a document before running this script."
else if errorNumber is 1001 then
set alertString to "SELECTION ERROR"
set errorMessage to "Please select a table before running this script."
else if errorNumber is 1002 then
set alertString to "MISSING DATA"
set errorMessage to "This script assumes the use of a single row header whose cell values will be used as field names in the created database."
else
set alertString to "EXECUTION ERROR"
end if
if errorNumber is not -128 then
display alert alertString message errorMessage buttons {"Cancel"}
end if
end try
end tell