Sign in to follow this  
Followers 0
Attckdog

Excel Format Cell as Text

8 posts in this topic

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

Share this post


Link to post
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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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)

Share this post


Link to post
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

Share this post


Link to post
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

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • houser747
      By houser747
      I have previously used _IEFormElementGetObjByName and _IEFormElementSetValue to enter text into a search box on a form and then submit the form.
      I am now trying to enter text into a search box which is not part of a form. 
      Here is the HTML from the website that i'm trying to enter the data on and then submit the search.
      <div class="row">
          <div class="form-group col-xs-12">
              <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_lblSearchText" for="input-search">Registreringsbeteckning</span>
              <div class="input-group col-xs-12">
                  <span id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_preSearchText" class="input-group-addon">SE -</span>
                  <input name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$txtSearchText" type="text" value="DTH" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_txtSearchText" class="form-control" />
              </div>
          </div>
      </div>
      <div class="row">
          <div class="form-group col-xs-12">
              <label class="sr-only" for="">Sök</label>
              <input type="submit" name="ctl00$FullWidthWithSubmenuContent$FullWidthContent$MainContent$AircraftRegistry$btnSearch" value="Sök" id="FullWidthWithSubmenuContent_FullWidthContent_MainContent_AircraftRegistry_btnSearch" class="btn btn-primary ladda-button" data-style="expand-right" />
          </div>
      </div>
      Many thanks in advance
      cheers
      Roger
    • breakbadsp
      By breakbadsp
      I  want to create a excel file from my script if it does not exist.
      _ExcelBookOpen throws error=2 if file does not exist, after this error i want to create new file at this point.
      can i use _FileCreate()?
      _Logger($sLogPath, "{INFO}------: Opening Excel File: " & $sExcelPath& "") While 1 Local $oExcelTestResult = _ExcelBookOpen($sExcelPath) If @error = 2 Then If not _FileCreate($sResExcelPath) Then MsgBox(0, "Error", "Error In Opening REsult Excel File: Error: " & String(@error)) _Logger($sLogPath, "{ERROR}------: Result Excel File does not exist.. tried to create new but :ERROR : " & String(@error) & "") ExitLoop Else _Logger($sLogPath, "{INFO}------: Result Excel File does not exist.. **Created New**: ") EndIf Else ExitLoop EndIf WEnd  
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have to store an entire row of a Excel workbook into an array.  The row index is stored in a variable.
      How can i do it?
      Thanks in advance for your support.
    • robcull
      By robcull
      Hello all! I have had some issues reading text from different types of windows, occasionally, specifically with controlgettext. 
      **Before I begin, I know there are better ways to do what I attempt in the example below. That's not the point of this post. The point is my issues with controlgettext. 
      I am about to cite an example with an application you may be familiar with called SpeedFan (v4.52). My problem is not specific to speedfan, it is simply the most recent and easily reproducible example I can think of. 
      So, the goal of the script below is to get a string of text containing the current fan RPMs from the highlighted control in the screenshot below (see "speedfan_control_details.png").

      Now, here's a simple script for grabbing the window handle and reading the text from that control: 
      $wintitle = "SpeedFan 4.52" $controlID = "197934" ;will be reformatted as "[ID:######]" $hwnd = wingethandle($wintitle) if @error<>0 then msgbox(0, "WinGetHandle", "FAILURE. @error="&@error) Exit EndIf $text = ControlGetText($hwnd, "", "[ID:"&$controlID&"]") if @error=1 then msgbox(0, "ControlGetText", "FAILURE. @error="&@error) ;failure returns "" and @error=1 Exit EndIf msgbox (0, "ControlGetText", "SUCCESS. @error="&@error &@CRLF& "$text="&$text) ;success returns string and @error=0 You'll see that the ControlGetText operation runs without error, however it does not capture any text from the control. If you explore the other controls in this one window, you'll find mixed results across the board. Neither the temps nor voltages can be read, while the log field and some other elements can be read. Even when you read the text from the whole window, those elements are not included in the visible nor hidden texts. 
       
      I have run into this issue many times in the past- inconsistencies in the ability of autoit to interact with certain controls. What is it which makes this text different than any other readable texts? Is there an alternate method of reading the text in the window/control which could work? Any and all info to help me solve this mystery and satisfy my curiosity would be greatly appreciated. 
      Thanks  -Rob C
      PS: Running Autoit v3.3.14.2 on Win7 Ultimate x64