Prateek Chahar
Prateek Chahar

Reputation: 1

Unable to access a google sheet by its id from the onEdit function of another google sheet

I am working on two google sheets

I want to make changes in a google sheet(2nd sheet) if there is any change in another google sheet(1st sheet).

It runs without any issues when I manually run the onEdit() function from the apps script of 1st sheet. But when I make a change in the 1st sheet, the onEdit function should be executed and changes should take place but there is no change in the 2nd sheet. I am new to scripting, is this possible?? Here is the code I am using in the 1st sheet's apps script :

function onEdit(){
  const secondSheet = SpreadsheetApp.openById('1IpAq_xy1LWXYu8CLMsxUtXHwTj-dhAc_zBB5HzWz00M');
  secondSheet.getActiveSheet().getRange('A1').setValue("first sheet was edited")
}

Upvotes: 0

Views: 57

Answers (1)

4thAnd1
4thAnd1

Reputation: 856

Setup OnEdit trigger

Your code has no errors and should work perfectly even by running the onEdit function manually or by using an Onedit trigger, and given that you are sure that your onEdit function runs perfectly when executed manually, the problem could be because you have possibly not set up a trigger which is what responsible on automating the execution of functions on Google Apps Script. You can setup a trigger by following the steps below:

Select Triggers option

From the left panel of Apps Script editor, select Triggers(alarm clock icon) Triggers's icon

Add Trigger

On the bottom right of the triggers' home screen, click the button that says 'Add Trigger'(see screenshot below) Add Trigger button

Select what function to run

After clicking the Add Trigger button, a new screen will pop up, which will contain the options necessary to successfully set up the Onedit trigger. On the first requirement, which says 'Choose which function to run', select the function name that will run once there are changes on the '1st sheet'. In your case, it should be the onEdit function that is written in your script.

Select what function to run

Deployment, Event source and Event type options

The following three options should be as follows: For the option that says 'Choose which deployment should run'. Keep it as 'Head'. For the 'Event Source' option select 'From spreadsheet' and for 'Event type' option, select On edit

Deployment,event source and event type options

Click the save button on the button right after setting up these parameters/options

Reference(s): Google Apps Script Triggers

Upvotes: 1

Related Questions