Extracting data from Excel and pasting into html

A pure AppleScript Version (no shell scripts)

set templateFile to read (choose file with prompt "Please select your template file") -- Choose your template html file
set createPath to (choose folder with prompt "Where should I create the product pages?") as Unicode text
set replaceSet to {"{{H1Data}}", "{{ProductSKU}}", "{{ProductName}}", "{{ProductSize}}", "{{ProductDescription}}", "{{ProductRetailPrice}}", "{{ProductYourPrice}}", "{{ProductPhoto}}", "{{ProductPageTitle}}"}

set {master_data, dummy} to {{}, {}}
tell application "Microsoft Excel"
	repeat with r_row from 1 to 300 --Cycles through 300 rows
		repeat with c_col from 1 to 10 --Gets the data from columns 1 thru 10
			copy value of cell c_col of row r_row of active sheet to the end of dummy
		end repeat
		set end of master_data to dummy
		set dummy to {}
	end repeat
end tell

repeat with dataSet in master_data
	set modStr to templateFile
	repeat with i from 1 to 9
		set modStr to srchRep(modStr, item i of replaceSet, item i of dataSet)
	end repeat
	set newFilePath to (createPath & item 10 of dataSet & ".html")
	set fileRef to (open for access file newFilePath with write permission)
	write modStr to fileRef starting at eof
	close access fileRef
end repeat

on srchRep(theStr, fndStr, repStr)
	set atid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {fndStr}
	set tempList to text items of theStr
	set AppleScript's text item delimiters to {repStr}
	set theStr to tempList as string
	set AppleScript's text item delimiters to atid
	return theStr
end srchRep

I also have one more shell version coming… this one using a sed command file rather than mulitple piped sed statements.

Fantastic!

However, when running this I get this error:

It seems to work, but some rows in my excel file break it.

If you run it on that same “record” does it break again? Does it break with both sed versions? Can you provide the values for the cells on the record that breaks?

I’ve got a system working, however there are two cells which contain price that do not seem to be getting replaced in my template file - {{ProductRetailPrice}} and {{ProductYourPrice}}

The script is:

set templateFile to read (choose file with prompt "Please select your template file") -- Choose your template html file
set createPath to (choose folder with prompt "Where should I create the product pages?") as Unicode text
set replaceSet to {"{{ProductSKU}}", "{{ProductName}}", "{{ProductPhoto}}", "{{ProductRetailPrice}}", "{{ProductYourPrice}}", "{{ProductPageName}}", "{{ProductPageTitle}}", "{{H1Data}}", "{{ProductDescription}}"}

set {master_data, dummy} to {{}, {}}
tell application "Microsoft Excel"
    repeat with r_row from 1 to 300 --Cycles through 300 rows
        repeat with c_col from 1 to 9 --Gets the data from columns 1 thru 9
            copy value of cell c_col of row r_row of active sheet to the end of dummy
        end repeat
        set end of master_data to dummy
        set dummy to {}
    end repeat
end tell

repeat with dataSet in master_data
    set modStr to templateFile
    repeat with i from 1 to 8
        set modStr to srchRep(modStr, item i of replaceSet, item i of dataSet)
    end repeat
    set newFilePath to (createPath & item 6 of dataSet & ".html")
    set fileRef to (open for access file newFilePath with write permission)
    write modStr to fileRef starting at eof
    close access fileRef
end repeat

on srchRep(theStr, fndStr, repStr)
    set atid to AppleScript's text item delimiters
    set AppleScript's text item delimiters to {fndStr}
    set tempList to text items of theStr
    set AppleScript's text item delimiters to {repStr}
    set theStr to tempList as string
    set AppleScript's text item delimiters to atid
    return theStr
end srchRep

The template is:

[code]

{{ProductPageTitle}}
<table width="750" border="0" align="center" cellpadding="0" cellspacing="0">
  
  <tr valign="top">
    <td colspan="3">
	<a name="top"></a>
		<!--#include virtual="/includes/header.html" -->
	</td>
  </tr>
  
  
  <tr valign="top" bgcolor="#CCCCCC">
    <td colspan="3">
		<!--#include virtual="/includes/topmenu.html" -->
		
	</td>
  </tr>
  <tr valign="top">
    <td width="170" rowspan="2" bgcolor="#EBF6E3" class="lowerleft">
		<!--#include virtual="/includes/search.html" -->		  
	 </td>
   </tr>
		
      </table>
	  
	<div align="center"> 
		<div class="tinyspace">.</div>
		<!--#include virtual="/includes/scanalert.html" -->
		<div class="tinyspace">.</div>
      </div>
      <table width="170" cellpadding="0"  cellspacing="0" bgcolor="#EBF6E3" class="global-navigation">
        <tr>
          <td>
		   	<!--#include virtual="/includes/productlist.html" -->			   
			<!--#include virtual="/includes/services.html" -->
		 	<!--#include virtual="/includes/left.html" -->
		 </td>
        </tr>
      </table>
    <!--PICOSEARCH_SKIPALLEND-->
    </td>
    <td width="425" rowspan="2"><table width="425" align="left" cellpadding="2" cellspacing="2" bgcolor="#FFFFFF" id="content">
        <tr>
          <td valign="top"><!-- InstanceBeginEditable name="MainContent" -->
              
              <div id="productPageContainer">
              <h1>{{H1Data}}</h1>
             	<div id="productBuyInfoContainer">
				<img src="/images/tpp/{{ProductSKU}}.jpg" width="140" height="185" class="productImage">
             	<div id="productBuyInfo">
				
             		<p id="productName">{{ProductName}}</p>
             		<!--<p>{{ProductSize}}</p> -->
             		<p class="productPricing">Retail Price: $ {{ProductRetailPrice}}<br />
             		<span class="productSalePrice">Your Price: $ {{ProductYourPrice}}</span></p>
             		
             		<a href="http://www.nativeremedies-store.com/ShoppingCart.asp?ProductCode={{ProductSKU}}"><img class="cartButton" src="../images/buttons/tppCartAdd1.gif" width="139" height="31" border="0"></a>
             		<a href="http://www.nativeremedies-store.com/ShoppingCart.asp?ProductCode={{ProductSKU}}&Qty.{{ProductSKU}}=3"><img class="cartButton" src="../images/buttons/tppCartAdd2.gif" width="139" height="31" border="0"></a>
             		
            	</div><!--close:productBuyInfo-->
				<div class="clearer"></div>
				<p class="productShipping"><strong>Shipping & Handling:</strong> Normal shipping (USPS, 3-5 business days anywhere with the USA) is $5.99 for 1st item; add $1 per additional item. We ship world wide via USPS, UPS and FedEx. If you have shipping questions please <a href ="/shippingpolicy.shtml">view our complete Shipping Policy</a>.</p>
				</div>
				
             	<p class="productHead"> <strong>Product Information</strong></p>
                <p>{{ProductDescription}} </p>
                <p class="productHead"> <strong>Product Ingredients</strong></p>
				<ul>
					<li>Ingredient</li>
				</ul>
                <div class="learnMore"><a href="#bottom" name="&lid=learnmore">Learn More »</a></div>
		  </div><!--close:productPageContainer-->

              <!-- InstanceEndEditable --></td>
        </tr>
    </table></td>
    <td width="150" height="200">
	<!--PICOSEARCH_SKIPALLSTART-->
	<!--#include virtual="/includes/newsletter.html" -->
	<!--#include virtual="/includes/policy.html" -->
      <!-- InstanceBeginEditable name="specials" --><table width="100%"  cellspacing="2" cellpadding="2">
space
Testimonials:

"You can imagine how glad I was to find something that works so well and which also does not interact with my heart medication!" - Sheila.

View all testimonials.
Add your own testimonial!

space
	<!--#include virtual="/includes/tellafriend.html" -->
space
	<!--#include virtual="/includes/info.html" -->

        <br>
    </div>
	
	<!-- InstanceBeginEditable name="RelatedLinks" -->
      <table width="100% align="center" cellpadding="3" cellspacing="0" class="relatedlinks">
        <tr>
          <td height="30" valign="top" class="relatedlinksHeader">People
            who purchased this also purchased: </td>
        </tr>
        <tr>
          <td valign="top"><p> <span><a href="../temp-product-pages-test/purecalm_for_anxiety.shtml" name="&lid=RN_purecalm">PureCalm</a> -
              Relieves anxiety and panic attacks<br>                    <a href="../temp-product-pages-test/mindsoothe_for_depression.shtml" name="&lid=RN_mindsoothe">MindSoothe</a> -
                    Relieves depression and anxiety  <br>
                <a href="../temp-product-pages-test/mindsoothejr-teen-childhood-depression-anxiety.shtml" name="&lid=RN_mindsoothejr">MindSoothe
              Jr.</a> - Relieves child depression (ages 3-15 yrs)<br>
                <a href="../temp-product-pages-test/serenite_for_sleep_insomnia.shtml" name="&lid=RN_sereniteplus">SerenitePlus</a> -
                          Natural remedy for insomnia<br><a href="../temp-product-pages-test/serenitejr-children-baby-sleep-disorder-insomnia.shtml" name="&lid=RN_serenitejr">

Serenite Jr. - Remedies child insomnia (6 months and up)

Nerve Tonic - Soothes anxiety levels

Serenite-LT - Relieves chronic insomnia for the long-term

LegCalm - Relieves Restless Leg Syndrome and Periodic Limb Movement
















Specials 25-30% Off
Eddy Ear

AquaRite

AdrenoBoost

MensReduce
Good through 3/31/2007


space













<!-- InstanceBeginEditable name="HBX_code" --><!--COPYRIGHT 1997-2005 WEBSIDESTORY,INC. ALL RIGHTS RESERVED. U.S.PATENT No. 6,393,479B1. MORE INFO:http://websidestory.com/privacy-->
[/code]

James, yes it produces this error on the same row over and over. Here’s the row data. Each line is a cell. The bottom one is the ProductDescription that seems to break it.

[code]ACN001

ClearSkin-A Gel

clearskinacnegel

30.95

21.95

clearskin-clear-skin-complexion-blemish-free

acne_gel.shtml

ClearSkin-A Gel for healthy, clear skin and trouble free complexion

Promotes healthy clear skin and encourages a smooth and trouble free complexion.

ClearSkin-A Gel - a 100% natural, proven and safe herbal gel that promotes healthy clear skin and encourages a smooth and trouble free complexion. Assists the skin’s natural ability to heal and remain blemish free.[/code]

So to update - the sed versions seem to be choking on the Product Description field in some rows.

The pure applescript version seems to not like the decimal point in the product retail price and product your price.

Okay for the sed version… let me guess on the record it chokes on there is a / characeter somewhere in the description? To get around that you would need to change the text delimiter in the script… so in the sed version replace all occurence of / with some character you would never find in the one of the cells… maybe a ~ or ^

In the AppleScript version make the following change

set modStr to srchRep(modStr, item i of replaceSet as string, item i of dataSet as string)

when you have a cell coming in from excel it’s being evaulated as a double not a string which throws off AppleScript text item delimiter parsing.

Hi, don’t mind if I share your script.
I am working on something similar to this but the different is I want the final data in XML not HTML. Also I don’t have a template.

The first part of the script works perfectly. I am able to get data/value from cells that I’ve defined. Secondly, I would need to populate them into a XML file with static tags.

For example, for every cell in first cell in every roll will be tagged as itemName, 2nd cell of every roll will be tagged as price…etc

it will look something like this:
A1 = itemName
B1= price
C1 = unit
…etc

I’ve read thread http://macscripter.net/viewtopic.php?id=26458 and it just completely drive me crazy.

Appreciate if anyone can help.

Thanks~