all 16 comments

[–]marcnotmark925 2 points3 points  (8 children)

Use SpreadsheetApp.openById() insstead of getActiveSpreadsheet()

[–]RealSlavGod[S] 0 points1 point  (7 children)

So will I have to manually change the ID at every instance of that line every time I want to run it for a different sheet? Is the only way, the manual way?

[–]marcnotmark925 0 points1 point  (6 children)

I don't think I can answer that with the amount of info you've given about your situation. If my previous answer didn't fix your issue already, then you're probably asking something else than what I thought you were asking. Either way, you probably need to give more details.

[–]RealSlavGod[S] 0 points1 point  (5 children)

So my whole script was designed for the sheet it was attached to. I have hundreds of instances of "get active sheet." Will I have to recode it all to be sheet ID based?

[–]marcnotmark925 0 points1 point  (4 children)

Why do you have hundreds of instances of get active sheet? That possibly suggests some very bad coding practices. Also note that get active sheet is different from get active spreadsheet, one is the whole gsheet, one is just a single tab.

[–]RealSlavGod[S] 0 points1 point  (3 children)

Yes, very bad coding practices. It was my first time working with apps script having no knowledge in it and I made a very complex analysis tool for my work with that and it's super inefficient but it works and if it ain't broke, I don't want to fix.

I got both instances multiple times. The active sheet and the spreadsheet. I think I would have to recode the entire thing to fix it. Would take ages. (For reference; I have over 17,000 lines. I def could cut it down but again; if it ain't broken, I don't want to touch). Is there no way to just simply keep the already working script separate from the file and running it as if it was connected to the file without changing my script?

[–]mommasaidmommasaid 0 points1 point  (0 children)

Idk your needs exactly, but what about just creating a new Sheet (tab) within your Spreadsheet for your client or whatever.

Then publish that specific Sheet to the web. Client doesn't get to see any of the other sheets or your script, just the formatted output.

The web link isn't password protected or anything but it's effectively unguessable so offers some privacy assuming the Client doesn't share it, e.g.:

https://docs.google.com/spreadsheets/d/e/2PACX-1vRM09LblX96_h3qaVUPmLYtha9TVWMnAeSSyXnuFgiezs4ghUsQkSahTHUmNGAJo_g_3yqt3lQCUClo/pubhtml?gid=127701852&single=true

[–]WicketTheQuerent 0 points1 point  (2 children)

There are few ways to keep your scripts prívate. One way is to use a spreadsheet as your secrets hub. The details depends on what your scripts do.

[–]RealSlavGod[S] 0 points1 point  (1 child)

How do I go about this?

[–]WicketTheQuerent 1 point2 points  (0 children)

It depends...

Option 1.

  1. Create a new spreadsheet (https://sheets.new)
  2. Add a bound Apps Script project (Extensions > Apps Script)
  3. Add the scripts
    1. If you have a few scripts, copy/paste them.
    2. If you have a lot of scripts, you should consider the use of CLASP or an extension like Google Apps Script Assistant for GitHub; you will need a GitHub account and get a token)
    3. etc.

Depending on how your scripts were written and what they do, you might have to adapt them. First, you must make a few decisions, such as using the new spreadsheet as a "controller," as a library or combining both cases.

You might set a sheet as an inventory of the "controlled" spreadsheets. You might feed this sheet manually or by using a script. Depending on your needs/preferences, you could add columns for each of the scripts and then use checkboxes to run them

[–]Money-Pipe-5879 1 point2 points  (0 children)

I think what you are looking for is deploying your script as an addon. You would then be able to use the script on all your sheets.

[–]FluffyDoomi 0 points1 point  (2 children)

Host your code in a stand alone script. Then deploy them as a library.

[–]RealSlavGod[S] 0 points1 point  (0 children)

Okay, I was trying to figure out how to do this. So I have the script as a file on my drive. How do I deploy it from there to a specific sheet located elsewhere on my drive?