1

I have an error in defining a command button in Google Apps Script. My statement is this, but there are syntax errors that I cannot resolve. Thanks for your help.

str = str + '<td><button onclick=' + 'google.script.run.setApproved(' + '"<?= outputHandle[i][0] ?>"' + ')' + 'id="ldap">' + "Approve" + '</button>'
raffaele
  • 51
  • 2
  • 9

1 Answers1

1

I thought that when I saw your script of str = str + '<td><button onclick=' + 'google.script.run.setApproved(' + '"<?= outputHandle[i][0] ?>"' + ')' + 'id="ldap">' + "Approve" + '</button>', it might be required to add a space between )i. So how about the following modification?

Modified script:

str = str + '<td><button onclick=' + 'google.script.run.setApproved(' + '"<?= outputHandle[i][0] ?>"' + ')' + ' id="ldap">' + "Approve" + '</button>'

or

str += '<td><button onclick=' + 'google.script.run.setApproved(' + '"<?= outputHandle[i][0] ?>"' + ')' + ' id="ldap">' + "Approve" + '</button>'

I can confirm that the above modification can be worked. And also, you can modify as follows.

str += '<td><button onclick="google.script.run.setApproved(\'<?= outputHandle[i][0] ?>\')" id="ldap">Approve</button>';
  • I added to ' id="ldap">'. It's from 'id="ldap">' to ' id="ldap">'.
  • From your script of '"<?= outputHandle[i][0] ?>"', it supposes that the value is string. Please be careful this.

Added:

And, when I saw your current script, I could understand your current issue of Uncaught SyntaxError: Unexpected token '<'. Unfortunately, your following script

str += '<td><button onclick="google.script.run.setApproved(<?= outputHandle[i][0] ?>)" id="ldap">Approve</button>';

cannot be used as the template. Because in your script, after the HTML data was loaded, the value is given to the above script by outputHandle of function onSuccess(outputHandle) {,,,}. So in this case, the scriptlet is not required to be used. I thought that this might be the reason for your current issue of Uncaught SyntaxError: Unexpected token '<'. In this case, please modify the above script as follows.

str += '<td><button onclick="google.script.run.setApproved(' + outputHandle[i][0] + ')" id="ldap">Approve</button>';

or

str += `<td><button onclick="google.script.run.setApproved(${outputHandle[i][0]})" id="ldap">Approve</button>`;
  • In this case, when the value of outputHandle[i][0] is the number value, the above modification can be used. But if the value of outputHandle[i][0] is the string value, please modify it as follows. Please be careful this.

      str += `<td><button onclick="google.script.run.setApproved('${outputHandle[i][0]}')" id="ldap">Approve</button>`;
    
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Grazie @Tanaike This seems to work formally. Now let's see if it calls the function. str += ''; – raffaele Dec 10 '21 at 14:58
  • Tanaike You're right, pass the non-variable string ( '"= outputHandle[i][0] ?>"') Tanaike how do I pass the variable? Thank you – raffaele Dec 10 '21 at 15:10
  • @raffaele Thank you for replying. When you want to give no string value with `'"= outputHandle[i][0] ?>"'`, how about modifying to `'= outputHandle[i][0] ?>'`? Or `str += '';` If this was not the direct solution, can I ask you about the sample value of `outputHandle[i][0]`? – Tanaike Dec 10 '21 at 23:49
  • Thanks for your patience. This str += ''; or this str += ''; gives me this error: Uncaught SyntaxError: Unexpected token '<' – raffaele Dec 11 '21 at 07:36
  • @raffaele Thank you for replying. I apologize for the inconvenience. Unfortunately, 1st script of your replying don't correctly use my proposed modification. Please test `str += '';` or `str += '';`. If this was not the direct solution, can I ask you about the sample value of `outputHandle[i][0]`? – Tanaike Dec 11 '21 at 08:02
  • thank you again. This item: str += ''; work fine but pass this parameter : = outputHandle[i][0] ?> while this: str += ''; have error: Uncaught SyntaxError: Unexpected token '<' – raffaele Dec 11 '21 at 08:55
  • @raffaele Thank you for replying. I apologize for the inconvenience. When `str += '';` is used with the number value of `outputHandle[i][0]`, no error occurs. But if the value of `outputHandle[i][0]` is the string value, an error occurs. Because the string value is used as the variable. But from `Uncaught SyntaxError: Unexpected token '<'`, it is considered that your this issue is from other part. So can you confirm the other part? If you cannot understand my English, please tell me. – Tanaike Dec 11 '21 at 11:49
  • @raffaele By the way, can you provide your script to correctly replicate your issue? – Tanaike Dec 11 '21 at 11:53
  • Thank you so much for your great patience @Tanaike. I understand your English and I also understand what you tried to tell me. Let's do this, if that's okay with you. I prepare a test environment and send you the link (tell me where) to log in and try. – raffaele Dec 11 '21 at 14:19
  • I have prepared test environment with script and database. Tell me how I send you the link – raffaele Dec 11 '21 at 14:37
  • @raffaele Thank you for replying. In that case, can you provide the simple sample script for replicating your issue? If you can do, please add it to your question. If you cannot do this, please tell me. – Tanaike Dec 11 '21 at 23:39
  • @raffaele Now I noticed that you had added your current script. I apologize for this. But in your post, you posted your current script as an answer. But it is not the answer. So please add it to your question. And please delete your answer. And, from your provided script, I added the modification point in my answer. Could you please confirm it? – Tanaike Dec 12 '21 at 08:48
  • I did not understand what you said. I think I'm wasting you too much time. I'm sorry. – raffaele Dec 12 '21 at 09:19
  • @raffaele Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand the relationship between my proposed answer and `I did not understand what you said. I think I'm wasting you too much time. I'm sorry.`. Can I ask you about the detail of it? I would like to try to understand your replying. – Tanaike Dec 12 '21 at 11:48
  • @raffaele From your provided script, I had added one more modified script. When you tested that, what result did you obtain? If that was not useful, I apologize again. – Tanaike Dec 12 '21 at 12:40
  • Hi @Tanaike, did you change this line? str += ''; I have this error: Uncaught SyntaxError: Unexpected token '<' – raffaele Dec 12 '21 at 15:21
  • 1
    @raffaele Thank you for replying. I have to apologize for my poor English skill. I had added a section "Added:". Please check it. In your script, it is not required to use the scriptlet. So please modify your `str = str + ''` of your original script to ``str += ``;`` and test it again. – Tanaike Dec 12 '21 at 22:48
  • Good morning @Tanaike. Great! Your solution now works perfectly. Meanwhile, I wanted to thank you for solving my problem, but above all for the great patience you had. Heartfelt thanks Tanaike! – raffaele Dec 13 '21 at 07:19