Skip to main content
Jake7732
Inspiring
September 11, 2018
Question

Create variables from coordinates array.

  • September 11, 2018
  • 1 reply
  • 544 views

I have an excel file like so:

OrderWODesign Qty

2551364

1

1248680

1

2551365212486813

I am exporting the file to a tab-delimited txt file.

I want to be able to make variables from this data like:

var order = ??;

var wo = ??;

var design = ??;

var qty = ??;

Some of my excel files can get up into the thousands and i want to be able to change the variables above for each row then run a function for every row.

Found this post but cant seem to get it working with the ptArr array.

Re: Excel data to construct illustrator object

From that post the ptArr array gives the x,y coordinates of the "cell" i am looking for.

How can i reference this so var order = ptArr(0,0); , var wo = ptArr(0,1); etc?

This topic has been closed for replies.

1 reply

Jake7732
Jake7732Author
Inspiring
September 11, 2018

I feel like every time I post a question here i answer it myself like an hour later.

Used the grid[row][column] as coordinates to the text document to get what i wanted.

#target illustrator 

function test(){ // enclosing function wraps the body of the script 

  if(app.documents.length < 1){ // this makes the script exit when there are no documents open 

    return; 

  } 

  var f = File.openDialog("Choose a CSV file.", "*.txt"); // choose a file with a .txt extension in Windows 

  if(!f){ 

    return; // exit script if the Cancel button was pressed 

  } 

  var gridStr; 

  f.open('r'); 

  gridStr = f.read(); // read the contents of the .txt file into a string variable 

  f.close(); 

 

  var grid = gridStr.split(/[\n\r]/); // turn the text into groups of rows by splitting the text by nextlines or carriage returns 

  for(var i=0; i<grid.length; i++){ // go through each of the row groups and turn them into groups of cells by splitting with tab characters 

   grid = grid.split(/\t/g); 

  }; 

 

 

  var doc = app.activeDocument; // set the active document to a variable 

  var ptArr = [], thisRow, x, y; // process the spreadsheet array to eliminate blank or incomplete rows 

  for(var i=1; i<grid.length; i++){ 

    if(typeof(grid[1]) == "undefined" || typeof(grid[2]) == "undefined" || grid[1] == "" || grid[2] == ""){ 

      continue; // if there are blank or missing cells where the x or y points should be, ignore this row 

    } 

    thisRow = grid

    x = thisRow [1].toString().replace(/"/g, '').replace(",", ".") * 1; // convert any nordic cells to numbers by removing any excel-generated quotes and replacing commas with periods, then cast to number by multiplying times one 

    y = thisRow [2].toString().replace(/"/g, '').replace(",", ".") * 1; 

    ptArr.push([x, y]); // add the processed coordinate point to array 

  }; 

 

for(var i=0; i<grid.length; i++){

var order = grid[0];

var wo = grid[2];

var design = grid[3];

var qty = grid[22];

}

alert(order);

alert(wo);

alert(design);

alert(qty);

}; 

test(); 

Thanks!

CarlosCanto
Community Expert
Community Expert
September 12, 2018

that's great, I like you're still trying to do it on your own while you wait.