How to store array elements in an Excel Sheet cell

2019-08-24 02:41发布

I want to store array elements in an Excel Sheet. How can I do that? Here is the sample code for an array:

for {set i 0} { $i < 3} {incr i} {
    set color($i) $i    
}

Now how can I store color(0), color(1), color(2) in different cells?

Edit: Based on this tcom example I have been trying to store array elements in an Excel Sheet.

标签: excel tcl
1条回答
该账号已被封号
2楼-- · 2019-08-24 02:56

Have a look at the commands array set and array get.

for {set i 0} { $i < 3} {incr i} {
    set color($i) $i    
}

set nvList [array get color]

Variable nvList has now the value 0 0 1 1 2 2 (mind the comments). With array get you get the Name-Value representation of an array. If you call array set you can convert nvList to an array again.

It this what you need?

Edit: Another example based on your comment:

# building example array with 100 elements
for {set r 1} {$r <= 100} {incr r} {
    set color($r) $r
}

set rows [array size color]
set columns {A B C}

for {set row 1} {$row <= $rows} {incr row} {
    foreach column $columns {
        $cells Item $row $column $color($row)
    }
}

Edit: Another (second) example based on your comment:

array set atten {...}
array set transmit {...}

set rows [array size atten]
for {set row 1} {$row <= $rows} {incr row} {
    $cells Item $row "B" $atten($row)
} 

set rows [array size transmit]
for {set row 1} {$row <= $rows} {incr row} {
    $cells Item $row "C" $transmit($row)
}
查看更多
登录 后发表回答