简体   繁体   中英

Google Apps Script Running a Script from Within Another Sheets Code

I have several sheets set up to do a variety of things. I have a control sheet that looks to do things with these sheets, one of which would be to run a script that is in the target sheet (so not the control sheet). Is there a way in which I can get the control sheet to run the target sheet script?

For example, within Target Sheet "TargetOne", I've got attached code that runs "SubroutineOne".

function SubroutineOne() {

    doSomething();

}

And in the Control Sheet I'd want to have something like this.

function ControlCode() {

    setSomething();

    run TargetSheet.SubroutineOne();

    reportSomething();

}

Is there anyway of doing that? Thanks for reading.

There is no direct way to do this. If you want to call other Apps Script projects you should import them as libraries.

To do this, publish the Script on the Target Sheet and use it. Take a look at the documentation for instructions on how to do this.

If you have multiple sheets, this can become a very convoluted solution. You can have a single "master" sheets instead and use the openById(id) function to access the sheets you want to manipulate. That also allows you to modify all the sheets from one script.

Yet another solution is to deploy the Target Sheet as a Web App and add the code you wish to execute on the doPost() or doGet() calls. Be careful with this solution, since you will have to implement controls to avoid duplicate calls or calls by someone else.

Lastly, you could try to use the Apps Script API to directly make the call to execute it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM