织梦CMS - 轻松建站从此开始!

欧博ABG-会员注册-官网网址

How to save data to Google SpreadSheet using Unit

时间:2025-08-05 23:06来源: 作者:admin 点击: 3 次
Previously, I was able to send data from a HTML Form to Google Form (Responses Spreadsheet) using ajax. Here's the code. Ajax Code function postCont

Previously, I was able to send data from a HTML Form to Google Form (Responses Spreadsheet) using ajax. Here's the code.

Ajax Code

function postContactToGoogle() { var email = $('#emailtosubscribe').val(); $.ajax({ url: "https://docs.google.com/forms/d/e/[key]/formResponse", data: { "entry_1064445353": email }, type: "POST", dataType: "xml", statusCode: { 0: function() { window.location.reload(); }, 200: function() { window.location.reload(); } } }); }

Now, I am trying to do the same in Unity using UnityWebRequest. Here's my code

Unity Code

public class SendToGoogle : MonoBehaviour { public GameObject email; private string Email; [SerializeField] private string BASE_URL = "https://docs.google.com/forms/d/e/[key]/formResponse"; IEnumerator Post(string json) { byte[] bytes = System.Text.Encoding.ASCII.GetBytes(json); using (UnityWebRequest www = new UnityWebRequest(BASE_URL, UnityWebRequest.kHttpVerbPOST)) { UploadHandlerRaw uH = new UploadHandlerRaw(bytes); DownloadHandlerBuffer dH = new DownloadHandlerBuffer(); = uH; = dH; ("Content-Type", "application/json"); yield return (); if () { Debug.Log(); } else { Debug.Log(()); Debug.Log(); } } } public void Subscribe() { Email = email.GetComponent<InputField>().text; var n = new JSONObject(); n["entry_1064445353"] = Email; string json = n.ToString(); Debug.Log(Email); StartCoroutine(Post(json)); } }

Now when I try to submit email from the Scene, time stamp is created in the spreadsheet but the email is not saved. I am doubtful on ("Content-Type", "application/json");, as in ajax I am using xml as datatype with dataType: "xml".

Moreover, I also tried sending just string without changing it to JSONObject and changing Content-Type on SetRequestHeader to xml.

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-08-06 03:08 最后登录:2025-08-06 03:08
栏目列表
推荐内容