cannot set active sheet

56 views
Skip to first unread message

Esteban Michel

unread,
Jul 14, 2020, 7:44:21 PM7/14/20
to Google Apps Script Community
Hi everyone.

I am sure it is a silly mistake but i can't find where it is.
I have this simple script to receive datas from my app and add them to a sheet called client in my spreadsheet.

But no matter what i try, i keep posting my data on the first sheet. (client being the fourth)

here is the code :

function doPost(e) {
   
  addClient
(e);
}


function addClient(e) {
 
var ss = SpreadsheetApp.openById("1lOkkIeMBtQn1Ok4-zqEqmmJNN1Ay-rf7OER8uZWOe9s");
 
SpreadsheetApp.setActiveSheet(ss.getSheets()[3]);
 
 
var name = e.parameter.client_name ;
 
var mail = e.parameter.client.email ;
 
var adress = e.parameter.client_adress ;
 
var zip = e.parameter.client_zip ;
 
var country = e.parameter.client_country ;
 
var tel = e.parameter.client_tel ;
 
var shipping = e.parameter.client_shipping;  
 
 
var lastrow = ss.getActiveSheet().getLastRow();
 
var id = ss.getActiveSheet().getRange(lastrow, 1).getValue()+1 ;

  ss
.getActiveSheet().appendRow([id,name,mail,adress,country,zip,tel,shipping]);
}

Thanks a lot

Alan Wells

unread,
Jul 14, 2020, 7:56:35 PM7/14/20
to Google Apps Script Community
Setting the active sheet isn't doing anything.
Get the fourth sheet directly.

var sh = ss.getSheets()[3];

  var lastrow = sh.getLastRow();
 
var id = sh.getRange(lastrow, 1).getValue()+1 ;

  sh.appendRow([id,name,mail,adress,country,zip,tel,shipping]);

Esteban Michel

unread,
Jul 14, 2020, 8:31:02 PM7/14/20
to Google Apps Script Community
Alright, sound reasonable yes :)
i'll try that.

But how is it different than to set active sheet to client
and then get active sheet.

I realisze in 2 operations instead of one so it suck but in the absolute, why does it not work.

i fail to understand that :/
What is the purpose of the setactivesheet() function then ?


Thanks a lot anyway :)

Esteban Michel

unread,
Jul 14, 2020, 8:37:17 PM7/14/20
to Google Apps Script Community
So i try the modification but it still doesn't work (yes i published the webapp :) ).

Here is the new code :

function doPost(e) {
   
  addClient
(e);
}


function addClient(e) {
 
var ss = SpreadsheetApp.openById("1lOkkIeMBtQn1Ok4-zqEqmmJNN1Ay-rf7OER8uZWOe9s");

 
var sheet = ss.getSheets()[3];

 
 
var name = e.parameter.client_name ;
 
var mail = e.parameter.client.email ;
 
var adress = e.parameter.client_adress ;
 
var zip = e.parameter.client_zip ;
 
var country = e.parameter.client_country ;
 
var tel = e.parameter.client_tel ;
 
var shipping = e.parameter.client_shipping;
 
 
 
var lastrow = sheet.getLastRow();
 
var id = sheet.getRange(lastrow, 1).getValue()+1 ;

  sheet
.appendRow([id,name,mail,adress,country,zip,tel,shipping]);
}


Alan Wells

unread,
Jul 14, 2020, 8:43:04 PM7/14/20
to Google Apps Script Community
If the spreadsheet isn't open, then there is no active sheet.
When you make a POST request, the spreadsheet could be closed,
but even if it is open, the published Web App doesn't "know" anything about the status of whether the document is open or not.
The only situation in which the code "knows" the status of the open document is when the Apps Script file is bound to the document,
and the user is running the code from the open document (Sheet in your case)
The only way that doPost can run is if the code is published as a Web App, and that's a different situation than code being
triggered from the user interface.

You'll need to do some troubleshooting.

Michael O'Shaughnessy

unread,
Jul 14, 2020, 9:29:29 PM7/14/20
to google-apps-sc...@googlegroups.com
Just adding my 2 cents...  
I have the habit of rearranging my tabs in a Google Spreadsheet.  Sometimes I like them in a different order....  What I do refrain from, especially if I have written any code, is change the name of a tab.  So if I name a tab "theData" then it will take a very compelling reason for me to change the name.  So in my code I get tabs by their name.  Something like this:

let ss = SpreadsheetApp.openById("big-id-string");
let ws = ss.getSheetByName("theData");

Hope this helps.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/952a8089-323d-4e2e-852c-619719dae4e6n%40googlegroups.com.

Esteban Michel

unread,
Jul 14, 2020, 11:20:44 PM7/14/20
to Google Apps Script Community
Yes i was opening my sheet by name before. I change to my current code to see if it would make a difference.
It didn't :)

I guess im in for some troubleshooting :)

Esteban Michel

unread,
Jul 14, 2020, 11:39:26 PM7/14/20
to google-apps-sc...@googlegroups.com
Another strange thing is that it still put the data in my "cashflow" sheet that is in first place even if i switch with the client sheet.

So now i have client on first place and cashflow on 4th and the incoming datas still arrive in cashflow.
How is that possible ?

Would it help if i attach the code to the spreadsheet ?

cbmserv...@gmail.com

unread,
Jul 15, 2020, 12:09:15 AM7/15/20
to google-apps-sc...@googlegroups.com

Since the code is in the doGet function, when you make any changes, you need to publish a new version of the webapp and not just update. I do not know why Google does not update the code if you just do an update, but certainly publishing a new version works.

--

You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

Esteban Michel

unread,
Jul 15, 2020, 1:16:47 AM7/15/20
to Google Apps Script Community
Thank you so much george. You saved my day.... i was really getting nut here.
So it worked by creating a new script and it got me thinking so i guess i have the answer to your wuestion...

I needed to increment the version. Moved from 1 to 2 and it worked like magic!!!


On Tuesday, July 14, 2020 at 10:09:15 PM UTC-6, George wrote:

Since the code is in the doGet function, when you make any changes, you need to publish a new version of the webapp and not just update. I do not know why Google does not update the code if you just do an update, but certainly publishing a new version works.

 

From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> On Behalf Of Esteban Michel
Sent: July 14, 2020 8:21 PM
To: Google Apps Script Community <google-apps-sc...@googlegroups.com>
Subject: [Apps-Script] Re: cannot set active sheet

 

Yes i was opening my sheet by name before. I change to my current code to see if it would make a difference.
It didn't :)

I guess im in for some troubleshooting :)

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-community+unsub...@googlegroups.com.

Michael O'Shaughnessy

unread,
Jul 15, 2020, 8:02:36 PM7/15/20
to google-apps-sc...@googlegroups.com
Oh my... sometimes it is the simple things...😀

Good catch Cbmservicesweb!!

On Wed, Jul 15, 2020 at 1:16 AM Esteban Michel <esteban....@gmail.com> wrote:
Thank you so much george. You saved my day.... i was really getting nut here.
So it worked by creating a new script and it got me thinking so i guess i have the answer to your wuestion...

I needed to increment the version. Moved from 1 to 2 and it worked like magic!!!

On Tuesday, July 14, 2020 at 10:09:15 PM UTC-6, George wrote:

Since the code is in the doGet function, when you make any changes, you need to publish a new version of the webapp and not just update. I do not know why Google does not update the code if you just do an update, but certainly publishing a new version works.

 

From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> On Behalf Of Esteban Michel
Sent: July 14, 2020 8:21 PM
To: Google Apps Script Community <google-apps-sc...@googlegroups.com>
Subject: [Apps-Script] Re: cannot set active sheet

 

Yes i was opening my sheet by name before. I change to my current code to see if it would make a difference.
It didn't :)

I guess im in for some troubleshooting :)

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.

To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/7d783942-e724-4b4a-be79-33f7fb647beao%40googlegroups.com.

cbmserv...@gmail.com

unread,
Jul 16, 2020, 4:21:58 PM7/16/20
to google-apps-sc...@googlegroups.com

Its because I also wasted lots of time thinking an update should “update the code” but it does not. This could be a bug at Google and maybe someone should open a ticket on it. But it has been like this for years..

Reply all
Reply to author
Forward
0 new messages