但未返回任何内容

但未返回任何内容

本文介绍了得到错误“脚本已完成,但未返回任何内容."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

该错误是正确的-考虑一下,如果没有为Webapp函数 doGet 提供任何参数,会发生什么情况.通过浏览器访问webapp时会发生这种情况.要解决此错误,您必须返回以下内容:

  function doGet(e){console.log({event:e});...如果(op)返回ContentService.createTextOutput(不支持的操作");别的返回HtmlService.createHtmlOutputFromFile(...);}} 

默认响应会因Webapp的使用方式而异-如果您打算仅将webapp用作原始后端,而计划将其托管在Web界面中.

如果webapp也要托管界面,则您的HTML应该使用内置的客户端-服务器通信( google.script.run ... )而不是 jQuery.ajax.详细查看 Apps脚本文档.这种模式将使您能够报告甚至处理服务器代码执行中的错误.

Actual web page image that should come I am getting error:

I have published new version of code but still getting the error after saving the code and error which is coming is "The script completed but did not return anything."

Code.gs

function doGet(e) {
  var op = e.parameter.action;
  var ss = SpreadsheetApp.openByUrl("some url");
  var sheet = ss.getSheetByName("Sheet1");

  if (op == "insert")
    return insert_value(e, sheet);

  //Make sure you are sending proper parameters
  if (op == "read")
    return read_value(e, ss);

  if (op == "update")
    return update_value(e, sheet);

  if (op == "delete")
    return delete_value(e, sheet);
}

//Recieve parameter and pass it to function to handle
function insert_value(request, sheet) {
  var id = request.parameter.id;
  var country = request.parameter.name;

  var flag = 1;
  var lr = sheet.getLastRow();
  for (var i = 1; i <= lr; i++) {
    var id1 = sheet.getRange(i, 2).getValue();
    if (id1 == id) {
      flag = 0;
      var result = "Id already exist..";
    }
  }

  //add new row with recieved parameter from client
  if (flag == 1) {
    var d = new Date();
    var currentTime = d.toLocaleString();
    var rowData = sheet.appendRow([currentTime, id, country]);
    var result="Insertion successful";
  }
  result = JSON.stringify({
    "result": result
  });

  return ContentService
    .createTextOutput(request.parameter.callback + "(" + result + ")")
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
  }

function read_value(request, ss) {
  var output  = ContentService.createTextOutput(),
      data = {};
  //Note : here sheet is sheet name , don't get confuse with other operation
  var sheet = "sheet1";

  data.records = readData_(ss, sheet);

  var callback = request.parameters.callback;
  if (callback === undefined) {
    output.setContent(JSON.stringify(data));
  } else {
    output.setContent(callback + "(" + JSON.stringify(data) + ")");
  }
  output.setMimeType(ContentService.MimeType.JAVASCRIPT);
  return output;
}


function readData_(ss, sheetname, properties) {
  if (typeof properties == "undefined") {
    properties = getHeaderRow_(ss, sheetname);
    properties = properties.map(function(p) { return p.replace(/\s+/g, '_'); });
  }

  var rows = getDataRows_(ss, sheetname),
      data = [];
  for (var r = 0, l = rows.length; r < l; r++) {
    var row = rows[r],
        record = {};

    for (var p in properties) {
      record[properties[p]] = row[p];
    }
    data.push(record);
  }
  return data;
}

function getDataRows_(ss, sheetname) {
  var sh = ss.getSheetByName(sheetname);
  return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
}


function getHeaderRow_(ss, sheetname) {
  var sh = ss.getSheetByName(sheetname);
  return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
}


//update function
function update_value(request, sheet) {
  var output  = ContentService.createTextOutput();
  var id = request.parameter.id;
  var flag = 0;
  var country = request.parameter.name;
  var lr = sheet.getLastRow();
  for (var i = 1; i <= lr; i++) {
    var rid = sheet.getRange(i, 2).getValue();
    if (rid == id) {
      sheet.getRange(i, 3).setValue(country);
      var result = "value updated successfully";
      flag = 1;
    }
  }
  if (flag == 0)
    var result="id not found";
  result = JSON.stringify({
    "result": result
  });

  return ContentService
    .createTextOutput(request.parameter.callback + "(" + result + ")")
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

function delete_value(request,sheet) {
  var output = ContentService.createTextOutput();
  var id = request.parameter.id;
  var country = request.parameter.name;
  var flag = 0;

  var lr = sheet.getLastRow();
  for (var i = 1; i <= lr; i++) {
    var rid = sheet.getRange(i, 2).getValue();
    if (rid == id) {
      sheet.deleteRow(i);
      var result = "value deleted successfully";
      flag = 1;
    }
  }

  if(flag==0)
    var result="id not found";
  result = JSON.stringify({
    "result": result
  });

  return ContentService
    .createTextOutput(request.parameter.callback + "(" + result + ")")
    .setMimeType(ContentService.MimeType.JAVASCRIPT);
}

HTML script section

<script>
  var script_url = "apps script webapp url";

  // Make an AJAX call to Google Script
  function insert_value() {
    $("#re").css("visibility","hidden");
    document.getElementById("loader").style.visibility = "visible";
    $('#mySpinner').addClass('spinner');

    var id1 = $("#id").val();
    var name = $("#name").val();
    var url = script_url + "?callback=ctrlq&name=" + name + "&id=" + id1 + "&action=insert";
    var request = jQuery.ajax({
      crossDomain: true,
      url: url ,
      method: "GET",
      dataType: "jsonp"
    });
  }

  function update_value(){
    $("#re").css("visibility","hidden");
    document.getElementById("loader").style.visibility = "visible";

    var id1 = $("#id").val();
    var name = $("#name").val();
    var url = script_url + "?callback=ctrlq&name=" + name + "&id=" + id1 + "&action=update";
    var request = jQuery.ajax({
      crossDomain: true,
      url: url ,
      method: "GET",
      dataType: "jsonp"
    });
  }

  function delete_value(){
    $("#re").css("visibility","hidden");
    document.getElementById("loader").style.visibility = "visible";
    $('#mySpinner').addClass('spinner');

    var id1 = $("#id").val();
    var name = $("#name").val();
    var url = script_url + "?callback=ctrlq&name=" + name + "&id=" + id1 + "&action=delete";
    var request = jQuery.ajax({
      crossDomain: true,
      url: url ,
      method: "GET",
      dataType: "jsonp"
    });
  }


  // print the returned data
  function ctrlq(e) {
    $("#re").html(e.result);
    $("#re").css("visibility","visible");
    read_value();
  }

  function read_value() {
    // Other code here to process the result
  }

</script>

HTML function calls:

<body>
<div align="center">
<h1>Operations .</h1>
<p>This is simple application<p>

<form >
ID
<input type = "text" name ="id" id="id">
Name
<input type = "text" name ="name" id="name">

</form>

<div id="loader"></div>

<p id="re"></p>

<input type = "button"  id = "b1" onClick="insert_value()" value = "Insert"></input>
<input type="button" onclick="read_value()" value="Read" />
<input type="button" onclick="update_value()" value="Update" />
<input type="button" onclick="delete_value()" value="Delete" />
<div id="showData"></div>
</div>
</body>
<div align="center">
</div>
<html>

It should show web page for performing operation shown in screenshotweb page:

解决方案

The error is correct - consider what happens when no parameters are given to your webapp function doGet. This scenario occurs when the webapp is visited via browser. To resolve this error, you must return something:

function doGet(e) {
  console.log({event: e});
  ...
  if (op)
    return ContentService.createTextOutput("Unsupported operation");
  else
    return HtmlService.createHtmlOutputFromFile(...);
  }
}

The default response will vary based on how your webapp is used - if you intend to use the webapp only as a crud backend vs. if you plan to have it host the web interface as well.

If the webapp is to host the interface as well, your HTML should use the built in client-server communication (google.script.run...) rather than jQuery.ajax. Review the Apps Script documentation in detail. This pattern will allow you to report and even handle errors in your server code execution.

这篇关于得到错误“脚本已完成,但未返回任何内容."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:48