Connecting Arduino with Spreadsheets - Google Sheets Setup

힘센캥거루
2025년 10월 8일(수정됨)
3
65

Recently, I decided to work with students to observe and analyze the temperature and humidity around the school using Arduino.
 
To store the data measured by Arduino, an SD card was necessary, and it was cumbersome to remove and reinsert the SD card to check the data.
 
I suddenly thought about saving the data on the web.
 
Today's post is the first step to connect Arduino with Google Sheets, specifically setting up Google Spreadsheets.

1. Google Spreadsheets

Google Spreadsheets is a free web app provided by Google.
 
It operates primarily in a browser and allows collaborative editing and viewing. Sheets can be modified via HTTPS requests using apps script.
 
First, search for 'Google Spreadsheet' on Google and create a new sheet.

Connecting Arduino with Spreadsheets - Google Sheets Setup-1

Upon logging in, you'll see a window like below. Click on the 'Blank Spreadsheet' to create one.

Connecting Arduino with Spreadsheets - Google Sheets Setup-2

If a new sheet is created as shown below, try the following steps:

  • Change the title and sheet name to English (to prevent potential errors).

  • Create column indexes for the data.

Connecting Arduino with Spreadsheets - Google Sheets Setup-3Connecting Arduino with Spreadsheets - Google Sheets Setup-4

Now, note the spreadsheet ID from the address bar. 

The format is '/d/Google Sheet ID/edit#gid=0'. Copy the middle part.

Connecting Arduino with Spreadsheets - Google Sheets Setup-5

The basic work on Google Spreadsheets is now finished. Let's set up the script for the spreadsheet next. 

2. Setting up App Script

Go to [Extensions] – [Apps Script] in the sheet menu.

Connecting Arduino with Spreadsheets - Google Sheets Setup-6

You will see a window like this. Now enter the desired code where the function is.

Connecting Arduino with Spreadsheets - Google Sheets Setup-7

Enter the following code. Here, you must specify the Google Sheet ID and the desired values' names using the switch, case statement.

function doGet(e) { 
  Logger.log( JSON.stringify(e) );
  var result = 'Ok';
  if (e.parameter == 'undefined') {
    result = 'No Parameters';
  }
  else {
    var sheet_id = 'Your Google Sheet ID'; // Spreadsheet ID
    var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
    var newRow = sheet.getLastRow() + 1; 
    var rowData = [];
    var Curr_Date = new Date();
    rowData[0] = Curr_Date; // Date in column A
    var Curr_Time = Utilities.formatDate(Curr_Date, "Asia/Tokyo", 'HH:mm:ss');
    rowData[1] = Curr_Time; // Time in column B
    for (var param in e.parameter) {
      Logger.log('In for loop, param=' + param);
      var value = stripQuotes(e.parameter[param]);
      Logger.log(param + ':' + e.parameter[param]);
      switch (param) {
        case 'value1':
        rowData[2] = value; 
        result = 'Entered value1 in 3rd column'; 
        break;
        case 'value2':
        rowData[3] = value;
        result += ' ,Entered value2 in 4th column'; 
        break;
        default:
        result = "unsupported parameter";
      }
    }
    Logger.log(JSON.stringify(rowData));
    var newRange = sheet.getRange(newRow, 1, 1, rowData.length);
    newRange.setValues([rowData]);
  }
  return ContentService.createTextOutput(result);
}
function stripQuotes( value ) {
  return value.replace(/^["']|['"]$/g, "");
}

Once the input is done, deploy the code. Click Deploy at the top right, then click on New Deployment.

Connecting Arduino with Spreadsheets - Google Sheets Setup-8

In the New Deployment, set the type to Web App, change users with access to 'Anyone', and click Deploy.

If you don't set permissions to 'Anyone', it will require login. Change the permissions back to 'Anyone' in that case.

Connecting Arduino with Spreadsheets - Google Sheets Setup-9Connecting Arduino with Spreadsheets - Google Sheets Setup-10

Clicking Deploy will request access permissions. Click Allow Access.

Connecting Arduino with Spreadsheets - Google Sheets Setup-11

When the access approval window appears, click your ID.

Connecting Arduino with Spreadsheets - Google Sheets Setup-12

Ignore the warning window and click Advanced below.

Connecting Arduino with Spreadsheets - Google Sheets Setup-13

Click "Go To ~" that appears below to grant permission.

Connecting Arduino with Spreadsheets - Google Sheets Setup-14Connecting Arduino with Spreadsheets - Google Sheets Setup-15

Once the new deployment window shows, copy the URL of the web app and confirm it has been properly set up.

Connecting Arduino with Spreadsheets - Google Sheets Setup-16

3. Checking the Web App

By sending a GET request to the URL of the web app, store the data in the query string to value1, value2.

In the address bar, append data like below to the end of the web app address, and press Enter. 

https://script.google.com/macros/s/Google App Script ID/exec?value1="DesiredValue1"&value2="DesiredValue2"
Connecting Arduino with Spreadsheets - Google Sheets Setup-17

This form of data transfer is known as a query string. Append desired values after "?" joined by "&".

Check Google Sheets to confirm the data is well-delivered.

Connecting Arduino with Spreadsheets - Google Sheets Setup-18

Simple numbers, not just text, can also be conveyed.

Connecting Arduino with Spreadsheets - Google Sheets Setup-19Connecting Arduino with Spreadsheets - Google Sheets Setup-20

The values are nicely entered.

4. Conclusion

In this post, we looked at how to enter data into Google Spreadsheets via a simple GET request.

All that's left is to send a GET request from Arduino using HTTPS communication.

I'll continue in the next post.

관련 글

Coding a Spider Robot with a micro:bit
Coding a Spider Robot with a micro:bit
This time, a free lecture was opened at a robotics technology meetup.It was a training session at Jaehyun High School on controlling a spider robot us...
Cloning an RFID Card with Arduino
Cloning an RFID Card with Arduino
Today I’m going to write about how to clone an RFID card with an Arduino.Once I write something down I don’t forget it, so I’m leaving this here as a...
Collecting Temperature and Humidity Data with Arduino ESP32
Collecting Temperature and Humidity Data with Arduino ESP32
Today, we will create a Wi-Fi Stevenson screen that measures temperature and humidity and transmits data using Arduino ESP32. This content is based on a script for a +1 session that will be conducted at school. 1. Materials The materials are simple. ESP32, DHT-22, three wires First, briefly explain the ESP32...
Using the Arduino D1 R2
Using the Arduino D1 R2
It could have been done using a regular Arduino Uno, but I decided to try the Wemos D1 R2 with built-in WiFi and struggled quite a bit. This article is for those using Arduinos like the D1 R2 with different pin mappings. 1. IDE Setting Due to the various types of Arduinos, you also need to install the board package suitable for each board...
Solution for Timed out waiting for packet header on MacBook with Arduino
Solution for Timed out waiting for packet header on MacBook with Arduino
How to solve the Timed out issue when connecting Arduino Wemos D1 R2 to a MacBook
Connecting Arduino with Spreadsheets - Configuring Code
Connecting Arduino with Spreadsheets - Configuring Code
In the last article, we explored the sheet setup required to connect Arduino with a spreadsheet. In this article, we'll learn about data transmission using HTTPS communication from the Arduino D1 board. 1. Installing Arduino D1 Board Library It is necessary to install the board library...

댓글을 불러오는 중...