Jump to content

Excel Format Cell as Text


Attckdog
 Share

Recommended Posts

I've got a tab delimited text file that needs column headers added.

As you might be able to see it's for an Amazon Order confirmation File.

With my current experience with autoit I figured the best way to make this happen would be to use excel.

Only problem is that excel is being a punk and changing my numbers to short hand.

The numbers that are being changed are FedEx Tracking numbers like: 064089467117955

End up like this: 6.40895E+13

So i was looking for a better way to do this or a way to prevent my cells from being changed.

Here's my Code:

FileCopy("Y:\New Folder\HOLDFILEEXPORT.txt", "C:\Amazon\HOLDFILEEXPORT.txt", 1)
FileCopy("C:\Amazon\HOLDFILEEXPORT.txt", "C:\Amazon\HOLDFILEEXPORT.xls", 1)

$oExcel = _ExcelBookAttach("C:\Amazon\HOLDFILEEXPORT.xls")
_ExcelRowInsert($oExcel, 1, 1)
Sleep(10)
_ExcelWriteCell($oExcel, "order-id", 1, 1)
Sleep(10)
_ExcelWriteCell($oExcel, "order-item-id", 1, 2)
Sleep(10)
_ExcelWriteCell($oExcel, "quantity", 1, 3)
Sleep(10)
_ExcelWriteCell($oExcel, "ship-date", 1, 4)
Sleep(10)
_ExcelWriteCell($oExcel, "carrier-code", 1, 5)
Sleep(10)
_ExcelWriteCell($oExcel, "carrier-name", 1, 6)
Sleep(10)
_ExcelWriteCell($oExcel, "tracking-number", 1, 7)
Sleep(10)
_ExcelWriteCell($oExcel, "ship-method", 1, 8)
Sleep(10)
_ExcelBookClose($oExcel)

A true renaissance man

Link to comment
Share on other sites

There is an extended Excel UDF available called with many useful functions extending the functionality of the UDF that comes with AutoIt.

Function _ExcelNumberFormat should do what you want.

Example: _ExcelNumberFormat($l_ExcelReport,"@","A1:A65536") formats the specified range as text.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Attckdog,

Find a better behaved copy of Excel...if it was working before the headers then the cell format changed.

kylomas

Edit: whoops, too slow, water beat me in...good luck!!

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

There is an extended Excel UDF available called with many useful functions extending the functionality of the UDF that comes with AutoIt.

Function _ExcelNumberFormat should do what you want.

Example: _ExcelNumberFormat($l_ExcelReport,"@","A1:A65536") formats the specified range as text.

Thanks for info. I can use it in the future too.

Do you know how to reformat whole column? How to specify range for whole column?

_ExcelNumberFormat($l_ExcelReport,"@","A1:A65536") ; formats the specified range as text.
Link to comment
Share on other sites

I'm not at my windows PC at the moment but IIRC you specify a column (e.g. column A ) as "A:A"

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@Attckdog

I don't get why you insist of going thru Excel (causing issues) just for the purpose of inserting a header line in a .CSV

First, AFAICT Amazon delivers CSV files with headers. At least it was the case until recently where we stopped selling there.

Now, if ever the CSV files you get miss the header line and all you want in the first place is have it, why not just prepending it to the CSV? That's 5 lines of code.

Finally, I guess you're going to process your CSV file somehow, presumably with AutoIt. If that's the case you don't even need the first step.

In any case, be warned that all platforms (Amazon, Paypal, eBay, YouNameIt) have great fun changing their CSV columns, headers and even semantics from time to time without any prior nor post notice. That means that you'll have to sanitize this kind of input with extreme caution at the risk of processing wrongly interpreted data, up to random garbage.

OMG I was talking about CSVs, but Amazon delivers TSV (tab separated values). That makes no difference anyway.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@Attckdog

I don't get why you insist of going thru Excel (causing issues) just for the purpose of inserting a header line in a .CSV

OMG I was talking about CSVs, but Amazon delivers TSV (tab separated values). That makes no difference anyway.

Yeah I just use the excel method because most of my experience with Autoit is in processing information from excel files.

So my first thing to attempt was to use what I already knew how to do.

What all would i use to edit the file in autoit without using excel stuff? I haven't done anything like that without excel files.

I'm not worried about the headers changing in Amazon because in the event of something not working properly it will just fail.

I'm only using these files to confirm with the tracking numbers. I will have to baby sit the script any how so no worries.

A true renaissance man

Link to comment
Share on other sites

Thanks water for the help. I eventually got the excel method to work.

After looking at the finished product I decided to scrap it. jchd was quite right. I was taking the hardest route.

After reading up on processing text files I found it was actually quite easy.

Here is the code I whipped up to use in place of what I was using.

Anyone is welcome to use it.

#include<File.au3>
Local $ReadFileArray
_FileReadToArray(@ScriptDir & "\Test.txt", $ReadFileArray)
FileWrite(@ScriptDir & "\TestResult!.txt", "order-id order-item-id quantity ship-date carrier-code carrier-name tracking-number ship-method" & @CRLF)
$file = FileOpen(@ScriptDir & "\TestResult!.txt", 1)
_FileWriteFromArray($file, $ReadFileArray, 1)
FileClose($file)

This will Make the confirmation file i needed with the desired column heads.

/Thead

A true renaissance man

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...