5분만에 구글 스프레드시트 채팅서버 구축하기





ChatScript.gs 소스입니다




var sheetId = SpreadsheetApp.openById("1vEIYwJxcvIPef04BImkU2SSzwgj-pPdf3L0S96J1VE8");
var sheet = sheetId.getSheets()[0];
var p;

function doPost(e) 
{
  p = e.parameter;
  
  chat();
}


function regCheck(val)
{
  var regExp = /[\=]/gi;

  if(regExp.test(val)) return false;
  else return true;
}


function chat()
{
  if(!regCheck(p.nickname) || !regCheck(p.chat))
    return;

  sheet.appendRow([p.nickname + " : " + p.chat]);
}


function clearTrigger()
{
  sheet.getRange("A1:A").clear();
}













GoogleChatManager.cs 소스입니다




using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;

public class GoogleChatManager : MonoBehaviour
{
    const string URL = "https://docs.google.com/spreadsheets/d/1vEIYwJxcvIPef04BImkU2SSzwgj-pPdf3L0S96J1VE8/export?format=tsv&range=B:B";
    const string WebURL = "https://script.google.com/macros/s/AKfycbw__P9vO30It5wKrPvSQdizWP8OyDU3OU50E1H-BorNF2RHMh0/exec";


    public Text ChatText;
    public InputField NicknameInput, ChatInput;




    void Start()
    {

#if !UNITY_ANDROID
        Screen.SetResolution(960, 540, false);
#endif
        StartCoroutine(Get());

    }


    IEnumerator Get() 
    {
        UnityWebRequest www = UnityWebRequest.Get(URL);
        yield return www.SendWebRequest();

        string data = www.downloadHandler.text;
        ChatText.text = data;

        StartCoroutine(Get());
    }



    public void ChatPost() 
    {
        WWWForm form = new WWWForm();
        form.AddField("nickname", NicknameInput.text);
        form.AddField("chat", ChatInput.text);

        StartCoroutine(Post(form));
    }


    IEnumerator Post(WWWForm form)
    {
        using (UnityWebRequest www = UnityWebRequest.Post(WebURL, form)) // 반드시 using을 써야한다
        {
            yield return www.SendWebRequest();
        }
    }
}