Skip to main content
Participant
March 1, 2016
Question

Please help! Need to adjust copy and paste table script.

  • March 1, 2016
  • 1 reply
  • 1003 views

Hi, I'm a novice to InDesign scripting and need some help to adjust this script so that it can help cut some manual labor time. This is the PopTabUnleashed script that I downloaded for free and used, but now I need to make some adjustments to it to fit a new need. This script currently copies the text from Table 2 and paste it into Table 1 without losing the formatting in Table 1, but it overwrites any existing text that's in Table 1. What I need it to do now is to be able to paste the text from Table 2 into Table 1 without overwriting the existing text, instead the text from Table 2 would flow right after the existing text in Table 1. Also I would like to add a " / " between each add. So the final result would look something similar to this for a table with 1 column, 3 rows:

Table 1:

English

English

English

Table 2:

French

French

French

Final Result:

English / French

English / French

English / French

Would I be able to just tweak this script or would a brand new script be required? Any help would be great!

Here's the existing script:

(function() {

  if (app.documents.length > 0 &&

  app.selection.length > 0) {

  var clipPref = app.clipboardPreferences.preferStyledTextWhenPasting;

  app.clipboardPreferences.preferStyledTextWhenPasting = true;

  $.sleep(100); // needed to give the clipboard preference time to take !!!

  app.doScript(popTab, undefined, undefined, UndoModes.entireScript, "Populate Table");

  app.clipboardPreferences.preferStyledTextWhenPasting = clipPref;

  }

  function popTab() {

  var selectedObj = validateSelection(app.selection[0]);

  if (selectedObj === null) return issueSelectMsg();

  // now we need to create a text frame to hold the contents of the clipboard

  // we could worry about the possibility of locked layers and the like, but really

  // but we should make sure it doesn't have a text wrap even though we're going to delete it

  var twPrefs = {textWrapMode : TextWrapModes.none};

  var tempFrame = app.documents[0].textFrames.add({textWrapPreferences : twPrefs});

  var tempStory = tempFrame.parentStory;

  app.select(tempStory.insertionPoints[0]);

  app.paste();

  if (tempStory.tables.length != 1) {

  tempFrame.remove();

  return issueClipMsg();

  }

  // Grab data from table into an array (c) of arrays

  var table = tempStory.tables[0];

  var cArray = Array();

  var cLim = table.columnCount;

  var rLim = table.rows.length;

  for (var c = 0; cLim > c; c++) {

  cArray = Array();

  for (var r = 0; rLim > r; r++) {

  var cell = table.cells.item(c + ":" +r);

  if (cell.name == (c + ":" + r)) {

  cArray = cell.texts[0].contents;

  } else {

  // cell is merged with earlier cell

  cArray = null;

  }

  }

  }

  tempFrame.remove(); // don't need it any more

  if (selectedObj.hasOwnProperty("columns")) {

  // selection is a range of cells so check we have the right amount of data

  var colMismatch = "";

  var rowMismatch = "";

  if (cLim != selectedObj.columns) {

  var colMismatch =  (cLim > selectedObj.columns) ?

  "more columns on clipboard than selected" :

  "columns on clipboard won't fill selection";

  cLim = Math.min(cLim, selectedObj.columns);

  }

  if (rLim != selectedObj.rows) {

  var rowMismatch = (rLim > selectedObj.rows) ?

  "more rows on clipboard than selected" :

  "rows on clipboard won't fill selection";

  rLim = Math.min(rLim, selectedObj.rows);

  }

  if (colMismatch + rowMismatch != "") {

  if (!confirm("Data mismatch: " + colMismatch +

  (colMismatch == "" ? rowMismatch : " and " + rowMismatch) +

  ".  Continue?")) {

  return;

  }

  }

  }

  // get base address of starting cell

  var cellNameParts = selectedObj.cell.name.split(":");

  var colBase = Number(cellNameParts[0]);

  var rowBase = Number(cellNameParts[1]);

  var table = selectedObj.table;

  // make sure we don't overflow bounds of table

  if (colBase + cLim > table.columnCount ||

  rowBase + rLim > table.rows.length) {

  if (!confirm("Data on clipboard overflows size of table; continue?")){

  return;

  } else {

  cLim = Math.min(cLim, table.columnCount - colBase);

  rLim = Math.min(rLim, table.rows.length - rowBase);

  }

  }

  var warning1 = new  Array(); // for overset cells with data

  var warning2 = new Array(); // for no data for cells not overset

  for (var c = 0; cLim > c; c++) {

  for (r = 0; rLim > r; r++) {

  var cell = table.cells.item((colBase + c) + ":" + (rowBase + r));

  var cellMerged = (cell.name != ((colBase + c) + ":" + (rowBase + r)));

  if (cArray != null) {

  if (cellMerged != true) {

  cell.contents = cArray;

  } else {

  // we have content for a merged cell

  warning1.push(cell.name);

  }

  } else {

  if (cellMerged == false) {

  warning2.push(cell.name);

  }

  }

  }

  }

  if (warning1.length > 0) {

  alert("Data provided for these cells merged with earler cells\n" + warning1.join("\n"));

  }

  if (warning2.length > 0) {

  alert("Because of merged cells on clip, no data provided for these cells\n" + warning2.join("\n"));

  }

This topic has been closed for replies.

1 reply

tpk1982
Legend
October 7, 2016

Did you solved this issue?