How can I POST a value to google sheets using Google App Script?

hi

I manage to post numerical data with the post block but not the character strings how should I proceed I am still looking

Share more details


hi

until now i sent numerical data in google sheets and it worked
I want to send text and it’s not working anymore
it’s a question of form I think but I haven’t found it yet

here are the blocks :

Capture d’écran 2022-09-04 à 09.17.27

1 Like

The Query Parameters for GAS (Google Apps Script) only accepts US-ASCII and your text has Ă« which is considered as an unsafe character.

You need to URL encode it.

[Edit]
Type this in the text and it should update the sheet correctly
Ma%C3%ABva

hi

I tried that before asking without success.

thank you for your support but I can’t see myself doing this for each first name or text :slight_smile:

1 Like

hi

I went through different posts and I found this one attended by a certain API Master Muneer :slight_smile:

https://community.thunkable.com/t/solved-can-you-help-me-make-a-post-to-shelly-using-x-www-form-urlencoded-calls/1890678

that seemed to correspond to what I wanted to do well it saves in the spreadsheet only the first parameter
while when I try my script live I can write what I want numbers accented words or not

I’m still looking for a solution

1 Like

You are using GAS (Google Apps Scrip) which has to ways to write to the sheet

  • doGet()
  • doPost()

When using doGet(), you have to pass the parameters as part of the URL (Query Parameters) and therefore you need to convert them to a form accepted by the URL scheme. This is the reason why you have to change the text to URLEncoded text.

However, when you use doPost() you will pass the data using the Body block and therefore you can use all type of text and numbers.

You can pass a JSON formatted data in text form using the Body component and in the Google script side read the text and convert it using JSON.parse() to use it as an object and update the sheet.

See a post using doPost().

1 Like

hi

so far i was consuming json i had saved in sheets or firebase
it gets stuck

I’m trying to send a simple json string

and I want the script to send it back to me and even that doesn’t work

Capture d’écran 2022-09-04 à 15.32.06

1 Like

You are using the ContentService to return text but var j is a JSON object not a text. If you want text to be return then you do not need to convert the e.postData.contents

The Failed to fetch data message requires to run the API from your phone not from the computer.

Follwoing is the easiest way to post data to Google sheet.

function doGet(e) {  
	Sheets.Spreadsheets.Values.append({"majorDimension":"ROWS","values": [[e.parameter.name ,e.parameter.age]]},
	[[ Google Sheet ID ]],
	 [[ Sheet name ]] + "!A:A",
	{valueInputOption:"USER_ENTERED"});
	return ContentService.createTextOutput("Updated");
}

The sheet needs to be Shared for this to work. Please note that I used name and age from your code. You can use the proper parameter names.

If your script is already inside the Google sheet then you can use the following:

function doGet(e) {  
	Sheets.Spreadsheets.Values.append({"majorDimension":"ROWS","values": [[e.parameter.name, e.parameter.age]]},
	SpreadsheetApp.getActiveSpreadsheet().getId(),
	SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getSheetName() + "!A:A",
	{valueInputOption:"USER_ENTERED"});
	return ContentService.createTextOutput("Updated");
}

This will add the values to the first sheet in the Google sheet file.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.