Google电子表格不会添加新行

时间:2017-12-19 10:42:30

标签: javascript google-apps-script google-sheets google-sheets-api

请帮助我,因为我被困住了。 我在googlesheets中使用这些tutorial来创建潜在客户。

那是我的codeGs

var SHEET_NAME = "sheet0637004101";

var SCRIPT_PROP = PropertiesService.getScriptProperties(); 

function doGet(e){
  return handleResponse(e);
}

function doPost(e){
   return handleResponse(e);
}

function handleResponse(e) {

var lock = LockService.getPublicLock();
lock.waitLock(30000);  

try {

   var doc = SpreadsheetApp.openById(SCRIPT_PROP.getProperty("key"));
   var sheet = doc.getSheetByName(SHEET_NAME);
   var headRow = e.parameter.header_row || 1;
   var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
   var nextRow = sheet.getLastRow()+1; // get next row
   var row = []; 

for (i in headers){
  if (headers[i] == "Timestamp"){ 
    row.push(new Date());
  } else {
    row.push(e.parameter[headers[i]]);
  }
 }
 sheet.getRange(nextRow, 1, 1, row.length).setValues([row]);
 return ContentService
      .createTextOutput(JSON.stringify({"result":"success", "row": nextRow}))
      .setMimeType(ContentService.MimeType.JSON);
} catch(e){

 return ContentService
      .createTextOutput(JSON.stringify({"result":"error", "error": e}))
      .setMimeType(ContentService.MimeType.JSON);
}   finally { 
    lock.releaseLock();
   }
}

function setup() {
   var doc = SpreadsheetApp.getActiveSpreadsheet();
   SCRIPT_PROP.setProperty("key", doc.getId());
}

myForm会

jQuery( document ).ready(function( $ ) {
	// variable to hold request
	var request;
	// bind to the submit event of our form
	$("#foo").submit(function(event){
		// abort any pending request
		if (request) {
			request.abort();
		}
		// setup some local variables
		var $form = $(this);
		// let's select and cache all the fields
		var $inputs = $form.find("input, select, button, textarea");
		// serialize the data in the form
		var serializedData = $form.serialize();
	
		// let's disable the inputs for the duration of the ajax request
		// Note: we disable elements AFTER the form data has been serialized.
		// Disabled form elements will not be serialized.
		$inputs.prop("disabled", true);
		$('#result').text('Sending data...');
	
		// fire off the request to /form.php
		request = $.ajax({
			url: "https://script.google.com/macros/s/AKfycbyD3AcUuKx7HARMabrer5TzPB4hn6VFcbKDIU36HqZWq6y27J0X/exec",
			type: "get",
			data: serializedData
		});
	
		// callback handler that will be called on success
		request.done(function (response, textStatus, jqXHR){
			// log a message to the console
			$('#result').html('<a href="https://docs.google.com/spreadsheets/d/1-i4WADh1j-bOR00cyt7XRD_E9H8Q9TE8kC15Bs1z9ro/edit#gid=0"_blank">Success - see Google Sheet</a>');
			console.log("Hooray, it worked!");
		});
	
		// callback handler that will be called on failure
		request.fail(function (jqXHR, textStatus, errorThrown){
			// log the error to the console
			console.error(
				"The following error occured: "+
				textStatus, errorThrown
			);
		});
	
		// callback handler that will be called regardless
		// if the request failed or succeeded
		request.always(function () {
			// reenable the inputs
			$inputs.prop("disabled", false);
		});
	
		// prevent default posting of form
		event.preventDefault();
	});
});
   
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<form id="foo">

    <p><label for="name">Name:</label>
        <input id="name" name="name" type="text" value=""></p>
    <p><label for="comment">Comment:</label><br>
        <textarea id="comment" name="comment" cols="40"></textarea></p>
    <p id="result"><a href="https://docs.google.com/spreadsheets/d/10tt64TiALYhPMqR2fh9JzkuhxW7oC0rXXPb_pmJ7HAY/edit?usp=sharing" target="_blank">Success - see Google Sheet</a></p>
    <input type="submit" id="submit-form" value="Send">

</form>

所以当我部署我的代码和'测试网络应用程序以获取最新代码'时。在codeGs中的结果是{“result”:“success”,“row”:5} 当我使用教程中的链接时,使用从我的测试页面发送的数据创建的新行,但是如果我正在尝试添加我在我的codeGs行中创建的链接未添加。但是在200之后浏览器首先进行了302操作即可。哪里我错了。 Heeeelp me pleease

0 个答案:

没有答案