Hi.
I have the script below to count words in all sheets and tables of a Number’s file. Nothing special. But sometimes the files I get came from Excel, so there are A LOT of empty columns and rows and the script takes forever testing them too.
I couldn’t think of a way to make it, for example, go to column C and check: “hum, even if this column has a header, I can see that rows 2 to 5 are empty, so I will assume this column is empty and won’t work on it”. And same with rows: "hey, I got to an empty row. How, there is another one following it, and another one, and another one… hey, maybe there is nothing from here to the last row, so I will stop working on this".
Does it make sense?
My code is below.
Any suggestion?
Thank you,
Luiz
tell application "Numbers"
-- Start the timer
set startTime to current date
set doc_name to the name of the front document
activate
set thisDocument to document 1
set textoFinal to ""
repeat with currentSheet in sheets of thisDocument
set nomeM to name of currentSheet
repeat with currentTable in tables of currentSheet
set rowCount to row count of currentTable
set columnCount to column count of currentTable
set nWords to 0
repeat with rowIndex from 1 to rowCount
repeat with columnIndex from 1 to columnCount
set columnLetter to character columnIndex of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
set cellRef to columnLetter & rowIndex
set thisCell to cell cellRef of currentTable
try
set theString to the value of thisCell
if theString is not missing value and theString is not "" then
set nWords to nWords + (count words of (the theString))
end if
end try
end repeat
end repeat
set textoFinal to textoFinal & nWords & tab & " words in sheet \"" & nomeM & "\"" & " of \"" & doc_name & "\"" & return as string
end repeat
end repeat
set f to POSIX file "/Users/XXXX/Desktop/Word Count.txt"
write textoFinal to f starting at eof
-- End the timer
set endTime to current date
-- Calculate the elapsed time in seconds
set elapsedTime to (endTime - startTime)
-- Display the elapsed time
display dialog "The script took " & elapsedTime & " seconds to run." giving up after 3
end tell