# How can I add columns in an excel file?

**URL:** <https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643>\
**Category:** Eggplant Functional\
**Tags:** excel\
**Created:** [October 30, 2023, 9:06pm UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643 "2023-10-30T21:06:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbedoy](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@rbedoy](https://forums.eggplantsoftware.com/u/rbedoy)\
**Post date:** [October 30, 2023, 9:06pm UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643/1 "2023-10-30T21:06:40Z")

</div>

I’m trying to automate a process we do on excel that includes adding columns to the excel file and paste data from an external txt file.  
I couldn’t find information about this on the Eggplant documentation.

Thanks!

---

<div class="post-metadata">

**Author:** ![GammaG](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@GammaG](https://forums.eggplantsoftware.com/u/GammaG)\
**Post date:** [November 6, 2023, 8:31am UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643/2 "2023-11-06T08:31:47Z")

</div>

Hallo rbedoy,  
I had same task to solve and found 2 ways.

1. Open EXCEL file as workbook and shift in each row each cell (i) to (i+1). It works, but it is very slow.
2. Build a short powershell command and call it via the shell of ep.  
New-Object -ComObject excel.application | %{ $_.workbooks.open(!filename!) | %{ $_.worksheets.item(!nnn!) |%{$cs=$_.columns(!F:F!); $cs.Insert}; $_.save(); $\_.close()}}  
Replace variables !..! acc. your needs. Take care about escape characters when calling the powershell via cmd.exe.

---

<div class="post-metadata">

**Author:** ![Karsten](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/k/e47c2d/32.png) [@Karsten](https://forums.eggplantsoftware.com/u/Karsten)\
**Post date:** [November 7, 2023, 10:23am UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643/3 "2023-11-07T10:23:41Z")

</div>

Hey Both,

Here is the documentation for Excel.  
Keep in mind that we can work in two ways with excel files:

1. [Work with Excel](https://docs.eggplantsoftware.com/studio/stk-excel-file-interaction/#working-with-excel-files-in-sensetalk) with worksheets, workbooks, cells etc
2. [Excel as database:](https://docs.eggplantsoftware.com/studio/stk-working-with-excel/) which provides you with all the commands that are available for databases. As the excel sheet is treated as one internally in Eggplant. [Adding records docu](https://docs.eggplantsoftware.com/studio/stk-writing-database/#add-record-add-records-command)

Here the example fro[m our docs for ad](https://docs.eggplantsoftware.com/studio/stk-writing-database/#the-is-a-record-operator)ding a record to an excel file via database connection

```auto
Set myDB to {type:"Excel", file:"~/Desktop/ClubData.xlsx", Writable:yes} -- Define the database connection, whether Excel or ODBC.
put table "Members" of myDB into MemberTable -- Pull the reference information for the table you need to modify
put {NameFirst:"Heidi", NameLast:"Mullers", StartDate:"2019-01-02"} into MemberToAdd -- This could also be done recursively using Iterators, if adding numerous members to the database table
add record MemberToAdd to table "Members" of myDB -- Add the record to the table in the database
Put the Record of MemberTable where "NameFirst" is "Heidi" into member3-- Retrieve the record you just added and store it in a variable
if member3 is a record then log "Success!" -- Verify that the record is an actual record with an active database connection

```

Cheers,  
Karsten

---

<div class="post-metadata">

**Author:** ![rbedoy](https://forums.eggplantsoftware.com/letter_avatar_proxy/v4/letter/r/77aa72/32.png) [@rbedoy](https://forums.eggplantsoftware.com/u/rbedoy)\
**Post date:** [November 7, 2023, 10:36pm UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643/4 "2023-11-07T22:36:44Z")

</div>

Karsten, thank you for your help, the problem of the example you shared is for adding a record so basically you are adding a row to the excel file, but I need to add a column, do you know how?

---

<div class="post-metadata">

**Author:** ![EggplantBlandine](https://forums.eggplantsoftware.com/user_avatar/forums.eggplantsoftware.com/eggplantblandine/32/203_2.png) [@EggplantBlandine](https://forums.eggplantsoftware.com/u/EggplantBlandine)\
**Post date:** [November 10, 2023, 8:38pm UTC](https://forums.eggplantsoftware.com/t/how-can-i-add-columns-in-an-excel-file/8643/5 "2023-11-10T20:38:56Z")

</div>

You can try this:

```auto
//This script will read from a spreadsheet and then write info to it
put "Data.xlsx" into filename
put "Sheet1" into WorkingTab

//Open the Excel file as an excel workbook and get the number of columns
set CurrentTab to Workbook(ResourcePath(filename)).Worksheet(WorkingTab)
Put the number of items in item 1 of CurrentTab.cellRange(1) into numberOfColumns
add 1 to numberOfColumns

//Add New Columns
put "New Column 1" into CurrentTab.Cell(1,numberOfColumns)
add 1 to numberOfColumns

put "New Column 2" into CurrentTab.Cell(1,numberOfColumns)
add 1 to numberOfColumns

```
