2017-04-06 8 views
0

私のスクリプトNumbersテーブルでは、copyコマンドを使用したことが原因と思われるリピートループで極端な(1セルあたり3〜4秒)私が理解しているように、copyは、setコマンドのような値を置き換えるのではなく、その値への新しい参照を作成するからです。私はcopyコマンド、私のMac、または一連のコマンドでループがうまく配置されているかどうかは分かりません。私は助けていないコマンドのシーケンスを切り替えてみました。同様のループの他の用途は、ゆっくりと動作しません。私の質問は一種の複合質問です。 copy以外のものを使うべきか、それとも単に私のMacですか? (メモリはハングを引き起こすのに十分なほど低いとは言えませんが、ハングアップ中に1.5GB残っています.Numberとスクリプトを除いて何もありません)まとめて、ハングの問題を解決するにはどうすればいいですか?リピートループで極端にハングする

ハングが始まるループの部分は、参照のための完全なスクリプトが場合に、ここに私の他のcopy用法のその配合問題である

-- This is where the extreme hang begins 

repeat with x from 1 to (count rows) - 2 
    repeat with y from 1 to count monthNames 
     copy incrementDates(y) of me to {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
     set monthStart to theDateString 
     set monthEnd to theMonthIndex & "/" & monthLengthInDays & "/" & theYear as text 
     set theFormula to "=SUMIFS(Amount,Category,A,Date, \">=" & monthStart & "\",Date, \"<=" & monthEnd & "\")" 
     set value of cell (y + 1) of row (x + 1) to theFormula 
    end repeat 
end repeat 

-- Extreme hang ends and script continues normally 

を下回っています。

set moneyInHeaders to {"Last Name", "First Name", "Receipt Number", "Payment", "Date", "Office", "City", "Referral Name"} 
set locationList to {"Location 1", "Location 2"} 
set the monthNames to {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} 
set moneyOutHeaders to {"Expense", "Amount", "Date", "Category", "Item"} 
set categoryItems to {"Marketing", "Software", "Tools", "Utilities", "Other"} 
set thisYearName to (year of (current date)) as string 
set moneyInColor to {31354, 51657, 22615} 
set moneyOutColor to {59623, 20816, 14391} 

tell application "Numbers" 
if not (exists document 1) then make new document 
tell document 1 
    delete every sheet 
    set the name of sheet 1 to "Money In" 
    tell sheet 1 
     delete every table 
     set moneyInTable to make new table with properties {name:"Money In", position:{-7, 29}, width:788, column count:count moneyInHeaders, row count:14, footer row count:1, header column count:2} 
     tell moneyInTable 
      set locationCount to count locationList -- get count of locations 
      set properties of row 1 to {background color:{moneyInColor}} 
      my populateCells(moneyInTable, row 1, moneyInHeaders, false) -- fill header row values 
      set value of last cell of column named "Payment" to ("=SUM(C)") 
      my populateCells(moneyInTable, column named "Office", locationList, true) -- fill pop up menu values 
      my formatCells(moneyInTable, column named "Payment", currency) 
      my formatCells(moneyInTable, column named "Date", date and time) 
      my formatCells(moneyInTable, column named "Office", pop up menu) 
      delete (rows 2 through (locationCount + 1)) -- remove rows that contain values so the pop up column is empty 
      repeat locationCount times -- add the rows back to keep table size 
       add row below row 2 
      end repeat 
     end tell -- end telling moneyInTable 

     -- Set locked false for now 
     set totalsTable to make new table with properties {name:"Money In Totals", locked:false, position:{785, 29}, column count:2, row count:(count monthNames) + 2, header column count:1, header row count:1, footer row count:1} 
     tell totalsTable 
      set properties of row 1 to {background color:{moneyInColor}} 
      my populateCells(totalsTable, column "A", monthNames, true) 
      set value of last cell of column "A" to "Grand Total" 
      set value of cell "B1" to "Totals" 
      set value of last cell to ("=SUM(B)") 
      tell column "B" 
       repeat with x from 1 to count monthNames -- loop and increment dates for the SUMIFS formula 
        copy incrementDates(x) of me to {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
        set monthStart to theDateString 
        set monthEnd to theMonthIndex & "/" & monthLengthInDays & "/" & theYear as text 
        set the value of cell (x + 1) to ("=SUMIFS(Payment,Date, \">=" & monthStart & "\", Date, \"<=" & monthEnd & "\")") 
       end repeat 
      end tell -- end telling column B 
     end tell -- end telling totalsTable 
    end tell -- end telling active sheet 

    make new sheet with properties {name:"Money Out"} 
    tell sheet 2 
     delete every table 
     set moneyOutTable to make new table with properties {name:"Money Out", position:{-7, 29}, width:710, column count:count moneyOutHeaders, row count:14, footer row count:1} 
     tell moneyOutTable 
      set catCount to count categoryItems -- get count of categoryItems 
      set properties of column "A" to {width:180} 
      set properties of row 1 to {background color:{moneyOutColor}} 
      set value of last cell of column "B" to ("=SUM(B)") 
      my populateCells(moneyOutTable, row 1, moneyOutHeaders, false) -- fill header row values 
      my populateCells(moneyOutTable, column named "Category", categoryItems, true) -- fill pop up values 
      my formatCells(moneyOutTable, column "A", text) 
      my formatCells(moneyOutTable, column named "Amount", currency) 
      my formatCells(moneyOutTable, column named "Date", date and time) 
      my formatCells(moneyOutTable, column named "Category", pop up menu) 
      my formatCells(moneyOutTable, column named "Item", text) 
      delete (rows 2 through (catCount + 1)) -- remove rows that contain values so the pop up menu is empty 
      repeat catCount times -- add the rows back to keep table size 
       add row below row 2 
      end repeat 
     end tell -- end telling moneyOutTable 

     set totalsTable to make new table with properties {name:"Money Out Totals", locked:false, position:{744, 29}, column count:2, row count:(count monthNames) + 2, header column count:1, header row count:1, footer row count:1} 
     tell totalsTable 
      set properties of row 1 to {background color:{moneyOutColor}} 
      my populateCells(totalsTable, column "A", monthNames, true) 
      set value of last cell of column "A" to "Grand Total" 
      set value of cell "B1" to "Totals" 
      set value of last cell to ("=SUM(B)") 
      tell column "B" 
       repeat with x from 1 to count monthNames -- oop and increment dates for the SUMIFS formula 
        copy incrementDates(x) of me to {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
        set monthStart to theDateString 
        set monthEnd to theMonthIndex & "/" & monthLengthInDays & "/" & theYear as text 
        set theFormula to ("=SUMIFS(Amount,Date, \">=" & monthStart & "\", Date, \"<=" & monthEnd & "\")") 
        set the value of cell (x + 1) to theFormula 
       end repeat 
      end tell -- end telling column B 
     end tell -- end telling totalsTable 

     set summaryTable to make new table with properties {name:"Category Totals", position:{943, 29}, column count:2, row count:(catCount) + 1} 
     tell summaryTable 
      set properties of row 1 to {background color:{moneyOutColor}} 
      set value of cell "B1" to "Totals" 
      my populateCells(summaryTable, column "A", categoryItems, true) 
      my formatCells(summaryTable, column "B", currency) 
      tell column 2 
       repeat with x from 2 to the count of cells 
        set theFormula to ("=SUMIFS(Amount,Category,A)") 
        set the value of cell x to theFormula 
       end repeat 
      end tell -- end telling column 2 
     end tell -- end telling summaryTable 

     -- Extreme hang starts on this table, specifically below where noted 

     set breakDownTable to make new table with properties {name:"Expenses By Month", column count:(count monthNames) + 1, row count:(count categoryItems) + 2, header column count:1, footer row count:1, header row count:1} 
     tell breakDownTable 
      set properties of row 1 to {background color:{moneyOutColor}} 
      my populateCells(breakDownTable, row 1, monthNames, true) 
      my populateCells(breakDownTable, column "A", categoryItems, true) 
      my formulateRow(breakDownTable, last row, "SUM") 
      set selection range to range "B2:M8" 
      set properties of selection range to {format:currency} 
      set value of last cell of column "A" to "Total" 

--***** This is where the extreme hang begins 

      repeat with x from 1 to (count rows) - 2 
       repeat with y from 1 to count monthNames 
        copy incrementDates(y) of me to {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
        set monthStart to theDateString 
        set monthEnd to theMonthIndex & "/" & monthLengthInDays & "/" & theYear as text 
        set theFormula to "=SUMIFS(Amount,Category,A,Date, \">=" & monthStart & "\",Date, \"<=" & monthEnd & "\")" 
        set value of cell (y + 1) of row (x + 1) to theFormula 
       end repeat 
      end repeat 

--***** Extreme hang ends and script continues normally 

      add column after last column -- add a totals column after everything else is done 
      set totalsColumn to the last column 
      tell totalsColumn 
       set value of cell 1 to "Total" 
       my formulateColumn(breakDownTable, totalsColumn, "SUM") 
      end tell -- end telling totalsColumn 
     end tell -- end telling breakDownTable 
    end tell -- end telling sheet 2 

    make new sheet with properties {name:"Overview"} 
    tell sheet 3 
     delete every table 
     set cashFlowTable to make new table with properties {name:"Cashflow", column count:2, row count:(count monthNames) + 2, footer row count:1} 
     tell cashFlowTable 
      my populateCells(cashFlowTable, column "A", monthNames, true) 
      set value of last cell of column "A" to "Grand Total" 
      my formatCells(cashFlowTable, column "B", currency) 
      set value of cell "B1" to "Cashflow Totals" 
      set value of last cell to ("=SUM(B)") 
      tell column 2 
       repeat with x from 1 to count monthNames 
        set value of cell (x + 1) to ("=Money In::Money In Totals::Totals " & item x of monthNames & "−Money Out::Money Out Totals::Totals " & item x of monthNames) 
       end repeat 
      end tell -- end telling column 2 
     end tell -- end telling cashflow table 
    end tell -- end telling sheet 3 
    set active sheet to first sheet 
end tell -- end telling document 
end tell -- end telling Numbers 

using terms from application "Numbers" 

to populateCells(theTable, theDirection, theListToUse, usingHeaders) 
    tell theTable 
     set x to 1 
     if usingHeaders is true then 
      repeat with x from 1 to count theListToUse 
       set value of cell (x + 1) of theDirection to (item x of theListToUse) 
      end repeat 
     else 
      repeat with x from 1 to count theListToUse 
       set value of cell x of theDirection to (item x of theListToUse) 
      end repeat 
     end if 
    end tell 
end populateCells 

to formatCells(theTable, theDirection, theFormat) 
    tell theTable 
     set theRange to ¬ 
      ((name of cell 2 of theDirection) & ":" & ¬ 
       (name of last cell of theDirection)) 
     set selection range to range theRange 
     set properties of selection range to {format:theFormat} 
    end tell 
end formatCells 

to colorCells(theTable, theDirection, theColor) 
    tell theTable 
     set theRange to ¬ 
      ((name of cell 2 of theDirection) & ":" & ¬ 
       (name of last cell of theDirection)) 
     set selection range to range theRange 
     set properties of selection range to {background color:theColor} 
    end tell 
end colorCells 

to formulateColumn(theTable, theColumn, theType) 
    tell theTable 
     tell theColumn 
      repeat with i from 2 to the count of cells 
       set thisRow to the row of cell i 
       set rangeStart to the name of cell 2 of thisRow 
       set rangeEnd to the name of cell -2 of thisRow 
       set theFormula to ("=" & theType & "(" & rangeStart & ":" & rangeEnd & ")") as string 
       set value of cell i to theFormula 
      end repeat 
     end tell 
    end tell 
end formulateColumn 

to formulateRow(theTable, theRow, theType) 
    tell theTable 
     tell theRow 
      repeat with i from 2 to the count of cells 
       set thisColumn to the column of cell i 
       set rangeStart to the name of thisColumn 
       set rangeEnd to the name of thisColumn 
       set theFormula to ("=" & theType & "(" & rangeStart & ":" & rangeEnd & ")") as string 
       set value of cell i to theFormula 
      end repeat 
     end tell 
    end tell 
end formulateRow 

end using terms from 

to incrementDates(tempMonth) 
    copy (current date) to tempDate 
    set day of tempDate to 1 
    set month of tempDate to tempMonth 
    set theDayName to weekday of tempDate 
    set theDayIndex to day of tempDate 
    set theMonth to month of tempDate 
    set theMonthIndex to theMonth as integer 
    set theMonthName to theMonth as string 
    set theYear to year of tempDate 
    set theDateString to short date string of tempDate 
    repeat with i from 1 to 32 
     set tempDate to tempDate + (1 * days) 
     if month of tempDate is not theMonth then 
      set monthLengthInDays to i 
      exit repeat 
     end if 
    end repeat 
    return {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
end incrementDates 
+0

コピー(現在の日付)を避けてください。代わりに使う:tempDateを現在の日付に設定する –

+0

@Pat_Moritaは少し曖昧だ。推論はしない? incrementDatesハンドラから返された値をどのように参照するのですか?あなたがコメントする時間を取るつもりなら、少なくともそれを意味のある、または有益なものにしてください。あなたのコメントは、「なぜ私の腕は傷ついていますか? 「あなたのもう片方の腕を使う」。 – Jesse

答えて

0

スクリプト上で見た後、私はそれを走ってきましたし、スクリプトを書きながら、そのためには、システムまたは複数のテストの実行に問題を残して記述しているの遅れは発生しませんでした。

次に、上記のコメントに記載されているように、お客様のincrementDatesハンドラーでcopyの代わりにsetコマンドを使用することができます。あなたはまだあなたが現在やっているような値を取得します、そこからそのためには

to incrementDates(tempMonth) 
    set tempDate to (current date) 
    set day of tempDate to 1 
    set month of tempDate to tempMonth 
    set theDayName to weekday of tempDate 
    set theDayIndex to day of tempDate 
    set theMonth to month of tempDate 
    set theMonthIndex to theMonth as integer 
    set theMonthName to theMonth as string 
    set theYear to year of tempDate 
    set theDateString to short date string of tempDate 
     repeat with i from 1 to 32 
     set tempDate to tempDate + (1 * days) 
     if month of tempDate is not theMonth then 
      set monthLengthInDays to i 
      exit repeat 
     end if 
    end repeat 
    return {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
end incrementDates 

に変更することができ(current date)あなたのハンドラが呼び出されるたびのコピー(12回ずつコール)を必要としません。

次に、特にハンドラを必要としないハンドラを作成しました。

to formatCells(theTable, theDirection, theFormat) 
    tell theTable 
     set theRange to ¬ 
     ((name of cell 2 of theDirection) & ":" & ¬ 
      (name of last cell of theDirection)) 
    set selection range to range theRange 
    set properties of selection range to {format:theFormat} 
end tell 
end formatCells 

上記差分

set properties of column named "Payment" to {format:currency} 

を使用するのと同じである無視できる程度であるが、私の一般的なガイドは、車輪を改革していないです。また、あなたが他のシートでそのテーブルを再作成する予定がある場合totalsTable

to makeMonthlyTotalsTable(theTable, theColor) 
    tell theTable 
     set properties of row 1 to {background color:{theColor}} 
     my populateCells(totalsTable, column "A", monthNames, true) 
     set value of last cell of column "A" to "Grand Total" 
     set value of cell "B1" to "Totals" 
     set value of last cell to ("=SUM(B)") 
     tell column "B" 
      repeat with x from 1 to count monthNames -- loop and increment dates for the SUMIFS formula 
       copy incrementDates(x) of me to {theDayIndex, theMonthIndex, theYear, theDateString, monthLengthInDays} 
       set monthStart to theDateString 
       set monthEnd to theMonthIndex & "/" & monthLengthInDays & "/" & theYear as text 
       set the value of cell (x + 1) to ("=SUMIFS(Payment,Date, \">=" & monthStart & "\", Date, \"<=" & monthEnd & "\")") 
      end repeat 
     end tell -- end telling column B 
    end tell -- end telling totalsTable 
end makeMonthlyTotalsTable 

ここでも無視できない違い、多分意見が、クリーンなコードを作成するときに、ハンドラを使用することができるハンドラを使用していません。

注:私はこのハンドラをテストしませんでした。スクリプトからコピーして貼り付け、変数名を渡すように変更しましたが、色名変数を設定して宣言する限り、正しく動作するはずですmonthNamesプロパティに。

property monthNames : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"} 
関連する問題