• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Variables

Community Beginner ,
Jul 02, 2023 Jul 02, 2023

Copy link to clipboard

Copied

Hi all,

I have a board of CADs (fashion garments) that I refer to frequently. Their are hundreds of CADS and they display a number of the units of stock we currently have on hand under each one. I want to pull this number from a frequently updated excel/CSV file, so as this doc updates it will refresh my Illustrator boards to reflect this.

 

Have read/watched a few tutorials. At the moment I have used a script to create my data sets, and have pulled in the info I need, what I am struggling with is then having multiple data sets working within the same document, for example the image below, I want it to show A colour/quantity/order number as one data set, and then the second colour/quantity/order number as another data set, all with different information.

Images below will hopefully make more sense than my rambling!

Script used:
https://github.com/Silly-V/Adobe-Illustrator/blob/master/Variable%20Importer/VariableImporter.jsx

If anyone has a solution or any alternate suggestions on how to do this would be so appreciated! Thanks so much in advance. 

TOPICS
How-to , Scripting

Views

291

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

Hi, unfortunately, that's not how variables work.

 

If I understood correctly, you want to update 3 sets of 3 text frames each.

 

If you have 3 variables, they would update 3 text frames or 1 set.

 

Perhaps you could batch create and export 1 set at time, then assemble them next to each other like you showed?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 03, 2023 Jul 03, 2023

Copy link to clipboard

Copied

Data merge of InDesign can be done with Multiple Record Layout settings. Is there a possibility to use InDesign?
https://helpx.adobe.com/indesign/using/data-merge.html

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 06, 2023 Jul 06, 2023

Copy link to clipboard

Copied

LATEST

Simple enough to do with a custom JavaScript.

 

Name your .ai file’s text frames “LABEL 1”, “LABEL 2”, “LABEL 3”, etc in the Layers palette, as shown in the screenshot, and re-save it. Save the script as “update template labels.jsx”.

 

With the .ai template open in Illustrator, choose File > Scripts > Other Script... and select the “update template labels.jsx” script. It will prompt you to select your .csv data file (its headings must match the 3 column headings shown in your screenshots), and will update the existing text frames with the new data.

 

 

 

// update template labels.jsx

// Read lines from a comma-separated text file and insert each line's values into a named text frame in the AI document.

// This code is released into the Public Domain.


(function() {


function parseCSVLine(data, headings) {
  // Parses one line of CSV data, returning it and the remaining data
  // data : string -- the CSV data
  // headings : [string] | undefined -- undefined when reading first (headings) line
  // Result: {fields: {string} | [string], data: string} --the line's fields plus any remaining lines of CSV data
  //
  // Notes: On return, fields is an object if headings array is given (headings are used as object keys), or an array of fields if no headings are provided. The number of headings must match the number of CSV fields, otherwise an error is thrown.
  var col = 0, eol = false; fields = headings ? {} : [];
  while (!eol) { // find next comma or double quote, or end of line
    var value = '', done = false, m = data.match(/[",]|[\r\n]+/);
    switch ((m || '')[0]) {
    case ',':
      value = data.slice(0, m.index);
      data = data.slice(m.index + 1); // step over text + comma
      break;
      
    case '"': // quoted field
      data = data.slice(1); // discard opening quote
      while (!done && data) {
        m = data.match(/"/);
        // note: quoted values can include line breaks so all we can do is read to next double-quote character
        if (!m) { throw new Error("Missing double-quote at end of CSV column " + col + "."); }
        var index = m.index;
        value += data.slice(0, index);
        if (data[index + 1] === '"') { // is the double quote followed by a second double quote (escape sequence)?
          value += '"'; // replace the 2 double quotes with 1
          index++;
        } else { // it's a single double-quote, which ends the quoted value
          done = true;
        }
        data = data.slice(index + 1); // step over quote(s)
      }
      data = data.slice(1); // step over comma
      break;
    
    default: // found end of line/end of file; last value is anything up to it
      if (m) {
        value = data.slice(0, m.index);
        data = data.slice(m.index + m[0].length); // step over text and comma
      } else {
        value = data;
        data = '';
      }
      eol = true;
    }
    if (headings) {
      var heading = headings[col];
      if (heading === undefined) { 
        throw new Error("Mismatched CSV columns: expected " + headings.length + " columns but found this value in column " + (col + 1) + ": '" + value + "'");
      }
      fields[heading] = value;
    } else {
      fields.push(value);
    }
    col++;
    if (col > 1000) { throw new Error("Too many CSV columns (this is probably a bug)."); }
  }
  if (headings && col !== headings.length) {
    throw new Error("Mismatched CSV columns (this is probably a bug).");
  }
  return {fields: fields, data: data};
}


function readFile(f) {
	f.encoding = 'utf-8';
  if (!f.open('r')) { throw new Error("Can't open file: " + f.fullName); }
  var s;
  try {
    s = f.read();
  } catch (e) {
    f.close();
    throw e;
  }
  f.close();
  return s;
}


// main

// prompt the user to select the CSV file manually each time:

var f = File.openDialog('Please choose the .CSV file:');

// OR, if the file is in a fixed location, replace the above line with this:

// var f = File('~/PATH/TO/INVENTORY.csv');

// (edit the path to the CSV file's actual location, e.g. '~/Documents/inventory.csv')

if (!f) { return; }

var data = readFile(f);

var headings = ['COLOR', 'QTY', 'ORDERNO']; // these MUST match the spreadsheet headings

data = parseCSVLine(data).data; // step over the headings line

var doc = app.activeDocument;

// read each line of the CSV and insert into text frame named 'LABEL 1', 'LABEL 2', 'LABEL 3', etc.
var i = 1;
while (data) {
  try {
	var result = parseCSVLine(data, headings);
	data = result.data;
	fields = result.fields;
	doc.textFrames.getByName('LABEL '+ i).contents = fields.COLOR + '\n' + fields.QTY + '\n' + fields.ORDERNO;
	i++;
	} catch (e) {
	  alert("Error processing CSV line " + (i + 1) + ": " + e);
	  return;
	}
}
alert("Updated " + (i - 1) + " labels."); // delete this line if you don't want the confirmation message

}());

 

 

 

I already had the functions written so it was just a matter of adding the loop and testing it; 15 minutes work (plus the time to write this post). If this solves your problem a $30 donation to a homeless charity of your choice is appreciated.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines