Android Question Problem with updating a cell in google sheet

Abbas818

Member
Hi
I am trying to communicate with Google sheet. Using the code below I can read data from Cell B3 of sheet One. This code works fine.



Dim j As HttpJob
Dim StrGS As String
j.Initialize("", Me)

j.Download("https://sheets.googleapis.com/v4/sp...3?key=AIzaSyCWv_klAv053_UDSW5SCqh2a7FPamSKRAc")

Wait For (j) JobDone(j As HttpJob)
If j.Success Then
StrGS=j.GetString
LblFromGS.Text=StrGS
End If
j.Release

I tried a lot to update the value of a cell in google sheet but not successful yet. I tested all examples in B4A forum but nothing worked for me. Can everybode help me. I just want a simple code to update the value of one cell for example B3.
 

DonManfred

Expert
Licensed User
Longtime User
check this library (not free):
 
Upvote 0

Abbas818

Member
Hi, thanks
It is not possible for me to buy it.
Is there another way?
 
Upvote 0

Abbas818

Member
Understood: I'm just trying to distil what the true problem is that makes it not possible for him to buy it, in case there is an easy solution to that secondary issue.

That library looks pretty good, btw, so thanks for the prompt pointing it out.
I can not pay by PayPal in my country. Only by Tether I can.
 
Upvote 0
Hi, thanks
It is not possible for me to buy it.
Is there another way?
You can use Google App Script (GAS) and publish the sheet as web app so you can access it by okhttp easily by WEB APP URL. Here is the GAS if you want:


Google App Script:
function doGet(e) {
  const sheet = SpreadsheetApp.openById('google sheet ID').getActiveSheet();
  const action = e.parameter.action;

  if (action === 'read') {
    const data = sheet.getDataRange().getValues();
    return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON);
  }

  if (action === 'create') {
    const values = JSON.parse(e.parameter.values);
    sheet.appendRow(values);
    return ContentService.createTextOutput('Row added successfully');
  }

  if (action === 'update') {
    const row = parseInt(e.parameter.row, 10);
    const values = JSON.parse(e.parameter.values);
    for (let i = 0; i < values.length; i++) {
      sheet.getRange(row, i + 1).setValue(values[i]);
    }
    return ContentService.createTextOutput('Row updated successfully');
  }

  if (action === 'delete') {
    const row = parseInt(e.parameter.row, 10);
    sheet.deleteRow(row);
    return ContentService.createTextOutput('Row deleted successfully');
  }

  return ContentService.createTextOutput('Invalid action');
}
 
Upvote 0

Abbas818

Member
Hi
Thanks for your response.
I create a new Spreadsheet and add your code to "Apps script" section in "Extensions" menu of the spereadsheet. I run it and allowed the required access to my google account.
This method is correct?
If yes, can you please show me how to update a cell in the spreadsheet using Okhttp?
 
Upvote 0
Click Deploy > New Deployment.
Select Web app.
Under "Who has access," select Anyone
Copy the Web App URL (it will look like this: https://script.google.com/macros/s/.../exec).
Please find attached b4j example for upating row 2. I have given all CRUD operations functions too for you to check.
 

Attachments

  • CRUDGoogleSheet.zip
    8.9 KB · Views: 2
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…