Sending data from Construct 2/3 to Google Spreadsheet

22

Attached Files

The following files have been attached to this tutorial:

.zip
.capx

posttogooglespreadsheet.capx

Download now 172.42 KB

Stats

12,153 visits, 18,886 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 13 Sep, 2018. Last updated 15 May, 2022

Sometimes you may need to collect data from players. This could be their scores, game statistics, feedback, survey answers or anything else.

There are many ways you can transmit this data - you can send it to MySQL database, upload to Firebase, email etc. Not many people know this, but it's also possible to send it to Google Spreadsheet.

DISCLAIMER: The method described below is not secure! The data is not encrypted or protected in any way, and anyone who has the web app link can send "fake" data to your spreadsheet. So don't use it for any sensitive information!

If you have multiple Google accounts, I recommend signing off all of them except just the one you want to be associated with this spreadsheet.

1. Open Google Sheets and create a new document. Enter the name of the document, for example "High scores".

Make sure your sheet name is "Sheet1" - this is important if you are using Google Docs in a language different from English.

Add the following headers into the first row:

Select the entire third column and click Format -> Number -> Date time.

2. Click Extensions -> Apps Script to create a new Google Apps Script

Rename it to "Submit Data to Google Sheet" or something similar.

Remove all existing code, open the file script.zip attached to this tutorial, unzip it and paste its content into the code.gs window.

Save changes.

3. On the top toolbar select "setup" function in the list and then click Run to execute the setup() function.

In the Authorization Required dialog, click on Review Permissions.

If you see this warning, click "Advanced" and then select "Go to <your script name> (unsafe)".

Confirm all permissions and click "Allow".

4. Click Deploy button on top and select "New deployment".

Make sure to select "New deployment" every time! You can archive old deployments if needed.

Set "Who has access: Anyone"

In the next dialog copy the "Current web app URL", you'll need it later:

5.

Now open the CAPX project attached to this tutorial.

Paste the copied web app url into the global variable web_app_url.

Run the project and test:

Congratulations, you did it!

If you wish to send more data fields, simply add more parameters to the url and edit these lines in the script:

After making these changes in the script make sure to deploy the web app again and select "New deployment".

And finally, if you want to read the data back from the spreadsheet, please see this tutorial: Create Online Database with Google Spreadsheet.

.ZIP
.CAPX

posttogooglespreadsheet.capx

Download now 172.42 KB
  • 77 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Hi dop2000, I've gotten this to work and it works well. However, how can I increase the amount of coloumns I'm submitting data to from C3? For example, date, location, name, DOB, etc.

    I've tried to copy and paste the lines of code and republish as app from google spreadsheet but nothing works...

    • Yes, add new lines to the script, for example:

      sheet.getRange(nextRow, 5).setValue(e.parameter["location"]);

      Re-deploy the app. Make sure to send this new data in URL parameters.

        • [-] [+]
        • 1
        • Mpr's avatar
        • Mpr
        • 1 points
        • (2 children)

        Hi! I'm trying to add some columns too, but i need a little help here :P How can I send new data in URL parameters? I need to add something in .capx, right? I tried something like this:

        1. I added new line in the script:

        sheet.getRange(nextRow, 4).setValue(e.parameter["xxx"]);

        2. I created new textbox on Layout 1 and named it Enterxxx

        3. In Event sheet I added line (in second event, before Ajax action):

        system -> set s to s & "xxx=" &Enterxxx.text

        As result in google spreadsheet I get data from Enterxxx textbox in column B (Player Score). For example I put player score = 10 and Enterxxx is z, and I get: 10xxx=z in column B. I want z in column D . How should I do it correctly?

        • Hi,

          I think you missed one "&" in front of "xxx"

          Try this:

          system -> set s to s & "&xxx=" &Enterxxx.text

            • [-] [+]
            • 1
            • Mpr's avatar
            • Mpr
            • 1 points
            • *
            • (0 children)

            Thanks, but it looks I missed something else... Now I get only score (10 in column B, column D is still empty)

            EDIT!!!!

            Wait, its working now! I think I published script without saving changes before :P I just saved changes and now everyting is ok :) Thanks again for this tutorial and quick answer!

      • Cheers dude. It was because I wasn't selecting 'new' when I deployed again. I brought you a couple of coffees.

  • Hi!dop2000 Thank you for tutorial! But I get this error when press button "Cannot call method getLastRow" How I can fix it?

    EDIT: False alarm, it works! If non-english language is set in your Google Sheets, then you should rename your sheet to "Sheet1"

  • I'm getting

    TypeError: Cannot read properties of null (reading 'getId')

    When trying to RUN the script. And after doing all steps still getting error when previewing it with your capx.

    Can you advise? Thanks

  • hi dop2000 any idea with the 405 error code?

  • Can I display Timestamp with Player's Timezone? Such as

    09/02/2023 17:53:42 GMT+8

  • Load more comments (60 replies)