1

I keep getting

ScriptError: We're sorry, a server error occured. Please wait a bit and try again.

whenever I try to run the following code:

macros.gs

/**   @OnlyCurrentDoc   */
// When the Sheet is opened, add the menu and create an item in the menu *** WORKING
function onOpen() {
  SpreadsheetApp.getUi()
      .createMenu('Menu')
      .addItem('Send Confirmation Email', 'createSideBar')
      .addToUi();
}


// Creates the side bar from the Index.html file and displays it *** WORKING
function createSideBar() {
  var form = HtmlService.createTemplateFromFile('Index').evaluate().setTitle('Send Confirmation Email');
  SpreadsheetApp.getUi().showSidebar(form);
}


// Test function where I make a request to my site *** NOT WORKING
function testerr(formObject) {
  UrlFetchApp.fetch('https://webhook.site/...');
  return "Complete!";
}

Form.html

<form id="sendEmailForm" onsubmit="handleFormSubmit(this)">

    <div class="form-group">
        <label for="row_number">Row</label>
        <input class="form-control form-control-sm" type="number" id="row_number" name="row_number" required>
    </div>

    <div class="custom-file">
        <input type="file" class="custom-file-input" id="images_input" name="images_input" multiple required>
        <label class="custom-file-label" for="images_input">Select images...</label>
    </div> 

    <button id="submit-btn" type="submit" class="btn btn-primary btn-md btn-block">Submit</button>

</form>

JavaScript.html

<script>
    // Prevent forms from submitting. *** WORKING
    function preventFormSubmit() {
        console.log('Entered preventFormSubmit()...');
        
        var forms = document.querySelectorAll('form');
        for (var i = 0; i < forms.length; i++) {
            forms[i].addEventListener('submit', function (event) {
                event.preventDefault();
            });
        }
        
        console.log('Exited preventFormSubmit()...');
    }
    window.addEventListener('load', preventFormSubmit); // *** WORKING

    // *** NOT WORKING
    function handleFormSubmit(formObject) {
        console.log('Entered handleFormSubmit()...');
        
        google.script.run.withFailureHandler(updateButton).testerr(formObject);
        google.script.run.withSuccessHandler(updateButton).testerr(formObject);
        
        console.log('Exited handleFormSubmit()...');
    }

    // *** WORKING
    function updateButton(str) {
        document.getElementById('submit-btn').innerHTML = str;
    }
</script>

I simply want to create sidebar form that when submitted will make a request to a site. The only issue is when you click on submit, the ScriptError will show up. I've pinpointed where the error is being thrown: when handleFormSubmit() is called after hitting submit, this line google.script.run.withFailureHandler(updateButton).testerr(formObject) causes the error to be thrown.

I tried commenting all the lines out in the testerr() function and replacing them with a simple return "Complete!"; line. Still, the error persists.

Why is this happening? I can't figure out why, any help is appreciated!

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Sami Junior
  • 115
  • 1
  • 8
  • What runtime are you using? Have you signed in a single Google account or in multiple accounts? Why are you calling `macros.gs` to your server-side code file? – Rubén Aug 17 '20 at 20:44
  • V8 Runtime. Single Google account. I don't get your last question. – Sami Junior Aug 17 '20 at 20:50
  • The default file uses `Code.gs` or a variant according the Google account language, `macros.gs` is a file name used for Google Sheets macros... since the V8 runtime is buggy, maybe using that filename could cause some problems. In order tor reproduce the problem it will be nice to know if you set the name manually or you are using a file created automatically as this affects the project manifest. – Rubén Aug 17 '20 at 20:57
  • @Rubén I just changed the name of the file from `macros.gs` to `Code.gs` and it still doesn't work. I followed the steps from here if it helps: https://developers.google.com/apps-script/guides/html/communication#forms – Sami Junior Aug 17 '20 at 21:02
  • Try removing the `input type="file"` – Rubén Aug 17 '20 at 21:20
  • Also try changing to the old runtime – Rubén Aug 17 '20 at 21:21
  • @Rubén OK so it worked when I commented out the `input type="file"` part! I'll try switching to the old runtime and keeping the `input type="file"` to see if this is a bug with V8. – Sami Junior Aug 17 '20 at 21:25
  • I added couple of links to my answer that might be helpful – Rubén Aug 17 '20 at 21:27
  • 1
    @Rubén yeap, switching to the old runtime fixed the issue. Thank you so much you saved me hours of mindless debugging! – Sami Junior Aug 17 '20 at 21:28

2 Answers2

1

I'm not sure if this will solve "all" the problems with your code, but...

Replace

google.script.run.withFailureHandler(updateButton).testerr(formObject);
google.script.run.withSuccessHandler(updateButton).testerr(formObject);

by

google.script.run
.withFailureHandler(updateButton)
.withSuccessHandler(updateButton)
.testerr(formObject);

The above because the original code is making two calls to the same server function. This could introduce some problems due to a "race condition" (any of the lines could finish before the other, and both will call the same client-side callback)


Maybe the combination of the new runtime(v8) and the use of input file="type" is causing problems. Try changing to the old runtime.

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Still doesn't work. In fact once I can get this thing to work, I'll only want to do `google.script.run.withSuccessHandler(updateButton).testerr(formObject)` (no need for the failure handler). I only added the failure handler to try to figure out what was wrong! – Sami Junior Aug 17 '20 at 20:49
1

Unfortunately, it seems that the issue you are encountering might be a bug.

What you can do in this case is to star the issue on Issue Tracker here by clicking the next to the issue number and post a comment as well saying that you are affected by the issue.

As a workaround, you can try and switch back to the old runtime.

ale13
  • 5,679
  • 3
  • 10
  • 25