Friday, May 9, 2008

Delimited Line Template

//***** LargeEdit Scripting *****
//
// Title: Delimited Line Template
// Author: Tim O'Brien
// Date: May 8, 2008
// Purpose: Merges lines of text containing delimited fields into a template.
// Usage: Create a SQL insert statement and merge the fields of text
// into the correct position for inserting into a database.
// Example: Insert into table(id, name, date) values ( {F1}, '{F3}', {F2} );
// the field tags {F1} will be replaces with the actual values.
//*******************************
var Delimiter = 9;
//Tab = 9, Comma = 44, Pipe = 124.
//(see LargeEdit | Tools | ASCII Codes | View ASCII Code, for a complete list.

function Run() {
LargeEdit.ResultLog(
' Start - Delimited Line Template ');

var tmp = '';
tmp = LargeEdit.InputBox(
'Delimited Line Template','Enter Template:','')
if (tmp.length <= 0) {
LargeEdit.ResultLog(
' Canceled - Enter a Template ');
return;
}

//Variables used
var line, newline;
var flds, fld, fldidx, lineidx, findtxt;
var newtext = "";

//Loop for Lines of text
for (var i = 0; i < LargeEdit.CurrentFile.LineCount; i++) {
line = LargeEdit.CurrentFile.Line( i );
lineidx = i +
1;
flds = line.split(String.fromCharCode(Delimiter));
//Tab default

//Replace Line Index Reference
newline = tmp; //Reset newline to base template
findtxt = '{LIDX}';
newline = newline.rep(findtxt, lineidx);

//Loop for Fields to do replacements
for (var j = 0; j < flds.length; j++) {
fld = flds[j]
//Get Field Value
fldidx = j + 1;
fld = fld.rep(
"'", "''");
findtxt =
'{F' + fldidx.toString() + '}';
newline = newline.rep(findtxt, fld);
}
newtext += newline +
"\r\n";

}

LargeEdit.New();
LargeEdit.CurrentFile.Text = newtext;

LargeEdit.ResultLog(
' Finish - Delimited Line Template ');

}

function StringReplace(findstr, replacestr) {
var regEx, strout;
regEx =
new RegExp(findstr, "igm");
strout =
this.replace(regEx, replacestr);
return strout;
}
String.
prototype.rep = StringReplace;

//copyright 2008 All rights reserved


No comments: