Select to view content in your preferred language

So I have this VBScript in labeling that I'm trying to convert to Arcade but didn't get it solution.

791
3
Jump to solution
12-14-2023 12:41 AM
Rajeshmadhaiyan
Emerging Contributor

Hello, 

Do you have any coveter tool if you have any please send me that,

Below mention the VB scripts code, Please someone can you help me that.

Dim result
If [Detect]= "N" and [Pre_Detect]= "N" then
result = [TextstringEdit] &vbnewline& [Post] &"<ITA>/"& [Pre_Post] &"</ITA>"
ElseIf [Detect]= "N" and [Pre_Detect]= "Y" then
result= [TextstringEdit] &vbnewline& [Post] &"<BOL><ITA>/"& [Pre_Post] &"</ITA></BOL>"
ElseIf [Detect]= "Y" and [Pre_Detect]= "N" then
result = [TextstringEdit] &vbnewline& "<BOL>"& [Post] &"</BOL>"&"<ITA>/"& [Pre_Post] &"</ITA>"
ElseIf [Detect]= "Y" and [Pre_Detect]= "Y" then
result = [TextstringEdit] &vbnewline& "<BOL>"& [Post] &"</BOL>"&"<BOL><ITA>/"& [Pre_Post] &"</ITA></BOL>"
Else result = "Error in result"
End If

__esri_field_calculator_splitter__
result

Thanks,

Rajesh M

remoterajesh@gmail.com

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

I don't know any automatic converters out there, but rewriting your conditions is simple enough.

Instead of "If condition then do something", you write "If(condition){do something}"

Text formatting in Arcade is easy as well. To build out a string, you can just separate your strings / string variables with "+" operators, and vbnewline can be replaced with textformatting.newline, <br>, or \n, depending on where the script is evaluating.

A direct translation would look like this:

var result

if ($feature.Detect == 'N' && $feature.Pre_Detect == 'N') {
  result = $feature.TextstringEdit + '\n' + $feature.Post + '<ITA>' + $feature.Pre_Post + '</ITA>'
} else if ($feature.Detect == 'N' && $feature.Pre_Detect == 'Y') {
  result = $feature.TextstringEdit + '\n' + $feature.Post + '<BOL><ITA>' + $feature.Pre_Post + '</ITA></BOL>'
} else if ($feature.Detect == 'Y' && $feature.Pre_Detect == 'N') {
  result = $feature.TextstringEdit + '\n<BOL>' + $feature.Post + '</BOL><ITA>' + $feature.Pre_Post + '</ITA>'
} else if ($feature.Detect == 'Y' && $feature.Pre_Detect == 'Y') {
  result = $feature.TextstringEdit + '\n<BOL>' + $feature.Post + '</BOL><BOL><ITA>' + $feature.Pre_Post + '</ITA></BOL>'
} else {
  result = "Error in result"
}

return result

 

But we can make some serious improvements to this in Arcade. For one, let's assign your attributes to variables. Instead of typing out "$feature.TextstringEdit" every time, we could have something shorter.

Second, we can use template literals for strings with variables piped in and do away with all of those + operators.

Here's what it looks like with those two things implemented. The code is taller, but not as wide, and a bit more readable.

var result

var detect = $feature.Detect
var predetect = $feature.Pre_Detect

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = $feature.Pre_Post

if (detect == 'N' && predetect == 'N') {
  result = `${tse}\n${post}<ITA>${prepost}</ITA>`
} else if (detect == 'N' && predetect == 'Y') {
  result = `${tse}\n${post}<BOL><ITA>${prepost}</ITA></BOL>`
} else if (detect == 'Y' && predetect == 'N') {
  result = `${tse}\n<BOL>${post}</BOL><ITA>${prepost}</ITA>`
} else if (detect == 'Y' && predetect == 'Y') {
  result = `${tse}\n<BOL>${post}</BOL><BOL><ITA>${prepost}</ITA></BOL>`
} else {
  result = "Error in result"
}

return result

 

But we can go further! Since you're just checking if those two fields are "Y" or "N", we can actually do the check in the variable assignment, which makes our "detect" and "predetect" variables booleans. Using them in a condition is much more concise that way. "predetect == 'N'" becomes "!predetect".

Further, a long if/else block that is being used to set a single parameter can be written with a When function. Long if/else blocks are better suited to cases where you are literally doing different things based on the condition.

var detect = $feature.Detect == 'Y'
var predetect = $feature.Pre_Detect == 'Y'

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = $feature.Pre_Post

return When(
  !detect && !predetect, `${tse}\n${post}<ITA>${prepost}</ITA>`,
  !detect && predetect,  `${tse}\n${post}<BOL><ITA>${prepost}</ITA></BOL>`,
  detect  && !predetect, `${tse}\n<BOL>${post}</BOL><ITA>${prepost}</ITA>`,
  detect  && predetect,  `${tse}\n<BOL>${post}</BOL><BOL><ITA>${prepost}</ITA></BOL>`,
  "Error in result"
)

 

And we can actually go one more step. Looking at what your expression is actually doing, it appears that when detect is 'Y', the "post" attribute is bold, and the same for pre_detect and "pre_post". Rather than doing an if/else or When series of conditions for each unique combination, we can do each of these separately, then combine them at the end.

var detect = $feature.Detect == 'Y'
var predetect = $feature.Pre_Detect == 'Y'

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = `<ITA>${$feature.Pre_Post}</ITA>`

var post_string = Iif(
  detect,
  `<BOL>${post}</BOL>`,
  post
)

var prepost_string = Iif(
  predetect,
  `<BOL>${prepost}</BOL>`,
  prepost
)

return `${tse}
${post_string} ${prepost_string}`

 

- Josh Carlson
Kendall County GIS

View solution in original post

Rajeshmadhaiyan
Emerging Contributor

Thank you so much! This is very helpful @Josh Carlson.

I will check it that code.

 

View solution in original post

0 Kudos
3 Replies
jcarlson
MVP Esteemed Contributor

I don't know any automatic converters out there, but rewriting your conditions is simple enough.

Instead of "If condition then do something", you write "If(condition){do something}"

Text formatting in Arcade is easy as well. To build out a string, you can just separate your strings / string variables with "+" operators, and vbnewline can be replaced with textformatting.newline, <br>, or \n, depending on where the script is evaluating.

A direct translation would look like this:

var result

if ($feature.Detect == 'N' && $feature.Pre_Detect == 'N') {
  result = $feature.TextstringEdit + '\n' + $feature.Post + '<ITA>' + $feature.Pre_Post + '</ITA>'
} else if ($feature.Detect == 'N' && $feature.Pre_Detect == 'Y') {
  result = $feature.TextstringEdit + '\n' + $feature.Post + '<BOL><ITA>' + $feature.Pre_Post + '</ITA></BOL>'
} else if ($feature.Detect == 'Y' && $feature.Pre_Detect == 'N') {
  result = $feature.TextstringEdit + '\n<BOL>' + $feature.Post + '</BOL><ITA>' + $feature.Pre_Post + '</ITA>'
} else if ($feature.Detect == 'Y' && $feature.Pre_Detect == 'Y') {
  result = $feature.TextstringEdit + '\n<BOL>' + $feature.Post + '</BOL><BOL><ITA>' + $feature.Pre_Post + '</ITA></BOL>'
} else {
  result = "Error in result"
}

return result

 

But we can make some serious improvements to this in Arcade. For one, let's assign your attributes to variables. Instead of typing out "$feature.TextstringEdit" every time, we could have something shorter.

Second, we can use template literals for strings with variables piped in and do away with all of those + operators.

Here's what it looks like with those two things implemented. The code is taller, but not as wide, and a bit more readable.

var result

var detect = $feature.Detect
var predetect = $feature.Pre_Detect

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = $feature.Pre_Post

if (detect == 'N' && predetect == 'N') {
  result = `${tse}\n${post}<ITA>${prepost}</ITA>`
} else if (detect == 'N' && predetect == 'Y') {
  result = `${tse}\n${post}<BOL><ITA>${prepost}</ITA></BOL>`
} else if (detect == 'Y' && predetect == 'N') {
  result = `${tse}\n<BOL>${post}</BOL><ITA>${prepost}</ITA>`
} else if (detect == 'Y' && predetect == 'Y') {
  result = `${tse}\n<BOL>${post}</BOL><BOL><ITA>${prepost}</ITA></BOL>`
} else {
  result = "Error in result"
}

return result

 

But we can go further! Since you're just checking if those two fields are "Y" or "N", we can actually do the check in the variable assignment, which makes our "detect" and "predetect" variables booleans. Using them in a condition is much more concise that way. "predetect == 'N'" becomes "!predetect".

Further, a long if/else block that is being used to set a single parameter can be written with a When function. Long if/else blocks are better suited to cases where you are literally doing different things based on the condition.

var detect = $feature.Detect == 'Y'
var predetect = $feature.Pre_Detect == 'Y'

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = $feature.Pre_Post

return When(
  !detect && !predetect, `${tse}\n${post}<ITA>${prepost}</ITA>`,
  !detect && predetect,  `${tse}\n${post}<BOL><ITA>${prepost}</ITA></BOL>`,
  detect  && !predetect, `${tse}\n<BOL>${post}</BOL><ITA>${prepost}</ITA>`,
  detect  && predetect,  `${tse}\n<BOL>${post}</BOL><BOL><ITA>${prepost}</ITA></BOL>`,
  "Error in result"
)

 

And we can actually go one more step. Looking at what your expression is actually doing, it appears that when detect is 'Y', the "post" attribute is bold, and the same for pre_detect and "pre_post". Rather than doing an if/else or When series of conditions for each unique combination, we can do each of these separately, then combine them at the end.

var detect = $feature.Detect == 'Y'
var predetect = $feature.Pre_Detect == 'Y'

var tse = $feature.TextstringEdit
var post = $feature.Post
var prepost = `<ITA>${$feature.Pre_Post}</ITA>`

var post_string = Iif(
  detect,
  `<BOL>${post}</BOL>`,
  post
)

var prepost_string = Iif(
  predetect,
  `<BOL>${prepost}</BOL>`,
  prepost
)

return `${tse}
${post_string} ${prepost_string}`

 

- Josh Carlson
Kendall County GIS
Rajeshmadhaiyan
Emerging Contributor

Thank you so much! This is very helpful @Josh Carlson.

I will check it that code.

 

0 Kudos
Rajeshmadhaiyan
Emerging Contributor

Converting VBScript to Arcade

 

So I have this VBScript in labeling that I'm trying to convert to Arcade, 

VBScribt expression: [TextStringEdit] &vbnewline&"<CLR black='100'>"& [Post]&"</CLR>"

 

Thanks,

Rajesh M

0 Kudos