首页 > 解决方案 > ScriptError:很抱歉,发生服务器错误。请稍等,然后重试

问题描述

我不断得到

ScriptError:很抱歉,发生服务器错误。请稍等,然后重试。

每当我尝试运行以下代码时:

宏.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!";
}

表单.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>

我只是想创建一个侧边栏表单,提交时会向站点发出请求。唯一的问题是当您单击提交时,会出现 ScriptError。我已经确定了错误被抛出的位置:当handleFormSubmit()在点击提交后被调用时,这一行google.script.run.withFailureHandler(updateButton).testerr(formObject)会导致错误被抛出。

我尝试将函数中的所有行都注释掉,testerr()并用一条简单的return "Complete!";行替换它们。尽管如此,错误仍然存​​在。

为什么会这样?我不知道为什么,任何帮助表示赞赏!

标签: google-apps-scriptgoogle-sheetsfile-uploadweb-applicationsserver-error

解决方案


我不确定这是否会解决您的代码的“所有”问题,但是......

代替

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

经过

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

以上是因为原始代码对同一个服务器函数进行了两次调用。由于“竞争条件”,这可能会引入一些问题(任何一行都可以在另一行之前完成,并且两者都将调用相同的客户端回调)


也许新的运行时(v8)和使用的组合input file="type"会导致问题。尝试更改为旧的运行时。

有关的


推荐阅读