Hi guys,
I'm tryng to make a simple app which can read and write data to a google sheet.
To do that I pass some parameters to a google script using SendHTTPResponse and adding a query string to the URL, then the Google Script makes some stuff with the google sheet and sends a response.
The app works perfectly running on windows, but, when I broadcast it or install via apk, the app doesn't communicate with the Google Script and doesn't return any error.
Here is an example code. It should append a row containing the string inserted in the editbox (GS adds timestamp)
AGK code:
SetErrorMode(2)
SetWindowTitle( "googlescript_test" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 )
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetSyncRate( 30, 0 )
SetScissor( 0,0,0,0 )
UseNewDefaultFonts( 1 )
CreateEditBox(1)
SetEditBoxPosition(1, 100, 100)
AddVirtualButton(1, 200, 200, 100)
global response$ = "none" //the response of te server
global GScriptURL$ = "/macros/s/AKfycbzpsKqGMRKNcKctMs-7xVXVCu0U8V8JhmoUAM416Qh-JIdaYXKm/exec" //the url of Google Script
do
if GetVirtualButtonPressed(1)
insertDB(GetEditBoxText(1))
endif
Print( ScreenFPS() )
Sync()
loop
function insertDB(id$) // id$ is the value to insert in the sheet
action$ = "&action=insert"
var$ = "?callback=Insertion&id="+HTTPEncode(id$) + action$ //query string with parameters to pass to GS
if GetTextExists(0) then DeleteText(0) //a text to show the query string
CreateText(0,var$)
SetTextY(0, 80)
SetTextSize(0,20)
http = CreateHTTPConnection()
SetHTTPHost( http, "script.google.com", 0)
SendHTTPRequestASync( http, GScriptURL$ + var$)
while GetHTTPResponseReady(http) = 0
Print( "Connecting..." )
Sync()
endwhile
response$ = GetHTTPResponse(http)
if GetTextExists(1) then DeleteText(1) //a text to show the response
CreateText(1, response$)
SetTextY(1, 120)
SetTextSize(1,20)
CloseHTTPConnection(http)
DeleteHTTPConnection(http)
endfunction
GS code (javascript):
function doGet(e){
var op = e.parameter.action;
var ss=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/10ZAvMDI6OvTBw9qHnCsF2a6qbV8dGUMgj6HkEIDZNDQ/edit#gid=0");
var sheet = ss.getSheetByName("Foglio1");
if(op=="insert")
return insert_value(e,sheet);
}
// INSERT VALUE
function insert_value(request,sheet){
var id = request.parameter.id;
var d = new Date();
var currentTime = d.toLocaleString();
var rowData = sheet.appendRow([id,currentTime]);
var result="Insertion successful";
result = JSON.stringify({"result": result});
return ContentService
.createTextOutput(request.parameter.callback + "(" + result + ")")
.setMimeType(ContentService.MimeType.JAVASCRIPT);
}
All the code is ready to run. You can copy AppGameKit code and directly try it with no need for changes because the GS and the sheet are public.
This is the link of the sheet:
https://docs.google.com/spreadsheets/d/10ZAvMDI6OvTBw9qHnCsF2a6qbV8dGUMgj6HkEIDZNDQ/edit#gid=0
I also tried to use SendHTTPRequest() and to set SetHTTPHost() HTTPS secure connection but nothing changes.
Posting var$ using SendHTTPRequest(http, GScriptURL$, var$) doesn't work neither on windows nor on android.
Am I doing it right? Any suggestion?
Thanks.