Search code examples
javascriptimacros

IMacros: SyntaxError: missing ; before statement


I have a problem when I change the code from IIM to JS I face this message "SyntaxError: missing ; before statement, line 5 (Error code: -991)"

original code is working perfectly

SET !DATASOURCE Pack_01.txt
SET !DATASOURCE_LINE 1
SET !VAR1 EVAL("var s=\"{{!COL1}}\"; s.split(\"@\")[1];")
PROMPT ID:{{!VAR1}}

but the js code is not working for my

var macro;
macro = "CODE:"; 
macro += "SET !DATASOURCE Pack_01.txt" + "\n";  
macro += "SET !DATASOURCE_LINE 1" + "\n";  
macro += "SET !VAR1 EVAL(\"var s=\\"{{!COL1}}\"; s.split(\"@\")[1];")" + "\n";  
macro += "PROMPT ID:{{!VAR1}}" + "\n";  
iimPlay(macro);

can you help, please!!


Solution

  • The syntax is not correct because your string contains double quotes. You need to escape them, replacing " with \".

    var macro;
    macro = "CODE:";
    macro += "SET !DATASOURCE Pack_01.txt" + "\n";
    macro += "SET !DATASOURCE_LINE 1" + "\n";
    macro += "SET !VAR1 EVAL(\"var s=\\\"{{!COL1}}\\\"; s.split(\\\"@\\\")[1];\")" + "\n";
    macro += "PROMPT ID:{{!VAR1}}" + "\n";
    iimPlay(macro);
    

    Another option is to use template literals:

    var macro = `CODE:SET !DATASOURCE Pack_01.txt
    SET !DATASOURCE_LINE 1
    SET !VAR1 EVAL("var s=\\"{{!COL1}}\\"; s.split(\\"@\\")[1];")
    PROMPT ID:{{!VAR1}}
    `;
    iimPlay(macro);