Jump to content

Phone Labels appearing when cell is empty


Fletch

Recommended Posts

I picked this rule in a post from 2013. What am I missing? The Labels (Office, Direct, Cell) still appear when the cell is empty. I should also add that I need to labels for each a particular font and color, and the actual phone numbers a separate font and color. I'll upload my csv file and a jpg image to show.

 

var numbers = ["Office: " + Field("Office"),"Direct: " + Field("Direct"),"Cell: " + Field("cell")];

return numbers.filter(function(s){return s.length>4;}).join(" ");

Edited by Fletch
Link to comment
Share on other sites

It's not working because you are filtering on the length being greater than 4. Your shortest length is "Cell: " which is 6.

 

Anyways, that wont work with the formatting you want. Give this a shot:

 

function format_phone(prefix, phone_num)
{
if (phone_num)
	return '<span color="Red">' + prefix + ": " + '</span><span color="Blue">' + phone_num + '</span>';
else
	return false;
}

var numbers = [format_phone("Office", Field("Office")), format_phone("Direct", Field("Direct")), format_phone("Cell", Field("Cell"))];

return numbers.filter(Boolean).join(" ");

 

 

Edit: The forums turned my "& # 32;" into a space (without the spaces between them). You will need to use that entity instead of a literal space character in the join(" ")

Edited by ThomasLewis
Link to comment
Share on other sites

That worked like a charm. I tweaked it a bit for the proper colors and to add a space between the numbers.

I also duped a section to add a extension to the Office number. I need the 'ext' to be black as well as the actual extension number, and a '.' after the ext instead of a ':' colon. Can that be done? (Code below)

 

function format_phone(prefix, phone_num)

{

if (phone_num)

return '<span color="PANTONE BLUE 072 U">' + prefix + ": " + '</span><span color="Black">' + phone_num + '</span>';

else

return false;

}

 

var numbers = [format_phone("<b> Office</b>", Field("Office")), format_phone("<b> ext</b>", Field("Ext")), format_phone("<b> Direct</b>", Field("Direct")), format_phone("<b> Cell</b>", Field("Cell"))];

 

return numbers.filter(Boolean).join(" ");

Link to comment
Share on other sites

I need the 'ext' to be black as well as the actual extension number, and a '.' after the ext instead of a ':' colon. Can that be done?

This should work:

function format_phone(prefix, phone_num)
{
   if (!Field(phone_num))
       return "";

   var endsWithDot = prefix.match(/\.$/);

   var result = '<span bold=true';

   if (!endsWithDot)
       result += ' color="PANTONE BLUE 072 U"';

   result +='>' + prefix;

   if (!endsWithDot)
       result += ":";

   result += " " + '</span>' + Field(phone_num);

   return result;
}

var numbers = { Office: "Office", "ext.": "Ext", Direct: "Direct", Cell: "Cell" };

var result = [];
for (var label in numbers)
   result.push(format_phone(label, numbers[label]));

return result.filter(String).join(" \n");

I've reduced the code a bit by having just the minimal data in the numbers object (which is now an object with a map of properties and values instead of an array), and calling the format_phone function in a loop, which includes the calls to Field. There's a little bit of Regular Expression magic to detect whether the prefix (label) ends with a dot, which is what we use to determine whether to change the color and append the colon. I also put the bold change into the span tag, and removed the code to set the color to Black, which is not needed if the variable for this rule is inserted in Black in the Text Editor, since the ending </span> tag restores the original color.

Link to comment
Share on other sites

That codes works perfectly! I do need one adjustment. I need the 'ext.' to be not bold.

Sure, a minor change:

function format_phone(prefix, phone_num)
{
   if (!Field(phone_num))
       return "";

   var endsWithDot = prefix.match(/\.$/);

   var result = '<span';

   if (!endsWithDot)
       result += ' bold=true color="PANTONE BLUE 072 U"';

   result +='>' + prefix;

   if (!endsWithDot)
       result += ":";

   result += " " + '</span>' + Field(phone_num);

   return result;
}

var numbers = { Office: "Office", "ext.": "Ext", Direct: "Direct", Cell: "Cell" };

var result = [];
for (var label in numbers)
   result.push(format_phone(label, numbers[label]));

return result.filter(String).join(" \n");

Link to comment
Share on other sites

Dan, thanks a million. I truly am grateful. For some reason, the 'ext.' is still Bold. I've examined the code to see if I could possibly figure it out and it's not really jumping out at me. So sorry. Can you adjust the code for the 'ext.' to not be bold?
Link to comment
Share on other sites

Dan, thanks a million. I truly am grateful. For some reason, the 'ext.' is still Bold. I've examined the code to see if I could possibly figure it out and it's not really jumping out at me. So sorry. Can you adjust the code for the 'ext.' to not be bold?

Hmm, it works for me. Though I don't have your job files, so I had to change the field names and use a different test job. Are you sure you have the exact code I posted? Anyway, there's nothing I can do to diagnose what's happening in your specific job without the files.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...