Extracting Data From Microsoft Excel

Hey guys, as there are several Excel to Website posts, I figured I'd ask for help here. I would like to save the entire cell contents for every row to a variable, and use those variables to fill out a web form. After each row, the script would run again with the new variables until the end. I am able to do the web form part ok, but I just haven't found a good post to help me accomplish Excel cell contents -> KM Variable per row.

Here's an example of the variables I'd need, in this case the script would run twice as there are two rows of variables.
56%20AM

Then, I'd have my custom web fill macros..

Does anyone have any ideas? Should I include this as a new topic? Thank you very much!!

Did you review the solution I provided in the original thread:

This looks very similar to your requirements. Of course you may need to edit the macro to fit your specific needs.

Questions?

1 Like

Hey Jay,

Since this is more about extracting data from Excel, I've moved it to a new topic.

When posting such requests please provide a proper data sample for testing – e.g paste-able text or a zipped Excel file. Paste-able should be fenced like so:

```text
Customer	Product Type	Name	Weight	Cost
Ronald’s Toys	Ball	BounceyBall	4	2
Toys Express	Stuffed Animal	Lion	25	7
```

It will render like this and won't have any funky artifacts:

Customer	Product Type	Name	Weight	Cost
Ronald’s Toys	Ball	BounceyBall	4	2
Toys Express	Stuffed Animal	Lion	25	7

In case it isn't obvious a zipped Excel file is the better choice of the two.

Here's how to take the selection in Excel into a Keyboard Maestro variable as a tab-delimited list of records:

tell application "Microsoft Excel"
   set selValList to value of selection
end tell

set AppleScript's text item delimiters to tab

repeat with i in selValList
   set contents of i to (contents of i) as text
end repeat

set AppleScript's text item delimiters to linefeed

set selValList to selValList as text

From there you can easily use a For Each Action to loop through each line/record.

-Chris

1 Like

Hey @JMichaelTX, yes thank you! I'm not an expert at regex and maybe that's what was throwing me off, but it was helpful to see that example!

@ccstone thanks a ton I think that approach works really well, and this other example can probably be used with it: Sending Emails from Row data individually

1 Like

Hey Chris, do you happen to have an example of how you'd get the variables out using a For Each loop? I'm thinking I need to take a regex class if that's the solution!

Hey Jay,

Yep. That's the solution if you want to be able to parse text.

Here's an example.

Make your selection in Excel (must correspond to the data structure in your example).

Hit F1 to start the macro.

Move on to the next record by pressing the N key.

Long press Q to Quit.

-Chris


Extracting Data from the Selection and Working with Each Record v1.00.kmmacros (12 KB)

1 Like

This is awesome!!! Clearest example on how to integrate excel with KM so far, you rock!!

1 Like

@jayknowstheway, here's an example macro created by using the other example as a starting point. I have also revised the RegEx to make it very easy to add/remove fields:
(?m)^([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)$

Each field (Excel cell) is represented by this sequence:
([^\t]+)

followed by either a TAB \t or End of Line $ metacharacter.

So the easiest way to change is at the beginning of the RegEx pattern.
To add more fields, insert ([^\t]+)\t immediately after (?m)^
To remove fields, delete ([^\t]+)\t immediately after (?m)^


Chris @ccstone gave you a good workable macro, but I have taken a different approach that does not require any AppleScript; and I have added in the For Each Action loop KM Actions Set Front Browser Form Field for each of the Web Form Fields. You will need to change each of these to select the action form field. You can just click on the image button to show a popup list of form fields.

The entire GROUP of form fields, and submit button, is disabled. After you change the form field Actions, you need to ENABLE this Group Action.

Questions?


Here's my example macro:

Example Output

image

image

image

image


MACRO:   Use Excel to Fill Chrome (or Safari) Form Fields [Example]

--- VER: 1.1    2019-01-31 ---

DOWNLOAD:

Use Excel to Fill Chrome (or Safari) Form Fields [Example].kmmacros (17 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Extract Multiple Rows/Fields from Excel and Set Web Form

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.

REQUIRES:

  1. KM 8.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
    .
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    • ALL Actions that are shown in the magenta color

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

Wow you are the greatest, I modified your macros and it got me exactly where I needed to be, this is a huge breakthrough for me!! Thank you so much! Both you and ccstone showed me great macro design techniques too. I am going to enjoy this weekend now and hope you have a blast as well :sunglasses::tropical_drink::snowflake:

1 Like

@jayknowstheway, If one of the above posts solves your problem/question as originally stated, please check the "Solved" checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.

1 Like

This is extremely helpful!

I do have an issue however, when cells contain no data. When encountering a cell with no data I would expect the script to return nothing for that variable, and then move to the next.

However, it actually misses it out, and then returns the next cell as the variable.

Example being Excel sheet containing the following data:

First Name, surname, height
John, Smith, 195cm
Jane, , 180cm
Frank, Jones, 188cm

Would return:

First Name: John
Surname: Smith
Height: 195cm

for the first row of data,

But

First Name: Jane
Surname: 180cm
Height:

for the second row of data.

Is there a fix for this?

Parsing text is tricky stuff, and the OP did not specify that there might be empty cells – nor was I paid for my work, so this simple example falls short of professional standards.

Yes, the problem can be worked around.

1 Like

Sorry, from my understanding of the RegEx I expected it to deal with the blank cells. I’ve been struggling to find a work around.

Very appreciative that you posted the solution to the OP - it’s been very useful!

Can we just thank cc for being such a superstar thank you, you definitely pushed my career to the next level. I didn't use KM for by projects but it helped me get into coding. You were a key part of my journey.

2 Likes

Post a testable data sample in the form of a zipped Excel worksheet – as well as the desired outcome in the form of a preformatted text block – and I'll take a look.

image

Here's a handy macro:

But don't use it for this job, because it will alter tabs in the pasted output.