3

I'm currently creating a spreadsheet where users can enter an item number and the sheet will return a description and price in the next two columns.

The spreadsheet pulls the item information from another sheet (not another page, rather a whole different URL) and the sheet updates itself every time an item number is entered (no vlookup because the info is on another URL).

Most likely, multiple people will need this form at the same time, and they will also need a copy for reference records. They all have access to the "Master File", the one I have, and I was hoping they could simply make a copy and then fill out the form.

However, while the code in my script works just fine on the Master File, when they make a copy the program won't run. I know it has to do with the triggers not being copied over and I've read up on writing triggers in the script, but here's the problem.

The users cannot see the script - that is, we don't want them to see any code. So they can't go in, turn on triggers via "Resources" or click the run/debug in script editor.

So basically I need a user to be able to open a shared, view only file, make a copy of a spreadsheet (which gets info from another sheet and has triggers), and use that spreadsheet buy inputting item numbers. Most of these people should not be able to see the inner workings, and wouldn't understand any of it anyway.

I was thinking a possible solution would be like what they do in this video around 25:56 or 37:355 where they can press a button and it writes the triggers. They don't go over how to do it though.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
mds93
  • 93
  • 1
  • 2
  • 9

2 Answers2

3

If your users are all in a private domain, your best solution would be to publish a private add-on (i.e. available only to domain users). That's not an option if you're using a consumer account.

Alternatively, you can use a menu-driven function to programmatically create the trigger(s) you need. This is effective in your case, because:

  • The "original" spreadsheet is shared as read-only, and users are expected to make a personal copy to enter their own data.
  • The users will be owners of their copies, so contained scripts will run as them and for them.

For example, you can try this shared spreadsheet. It's shared public, read-only, but if you save a copy, you will see a Custom Menu that sets a trigger function, and updates the first cell in the spreadsheet. Ten seconds late, the trigger function updates it again.

The demo script is contained in the spreadsheet, so you can see it for yourself there. Here's all it contains:

// Create a menu that will initialize the trigger
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Menu')
      .addItem('Initialize spreadsheet', 'setTrigger')
      .addToUi();
}

function setTrigger() {
  // clear any existing triggers
  var triggers = ScriptApp.getUserTriggers(SpreadsheetApp.getActive())
  for (var i=0; i<triggers.length; i++) {
    ScriptApp.deleteTrigger(triggers[i]);
  }

  // set new trigger
  ScriptApp.newTrigger("runTrigger")
           .timeBased()
           .after(10*1000)    // 10s delay
           .create();
  announce("Trigger set. Wait for it...");
}

function runTrigger() {
  announce("Trigger fired! This completes our demo.");
}

// Update first cell in spreadsheet
function announce(message) {
  var range = SpreadsheetApp.getActive().getSheets()[0].getRange("A1");
  range.setValue(message);
}

Instead of a menu, you could include a "button" image, and link a script to that. I didn't look at the video, but that's probably what they did. You can see more about that (silly, imho) option in How do you add UI inside cells in a google spreadsheet using app script?

Community
  • 1
  • 1
Mogsdad
  • 44,709
  • 21
  • 151
  • 275
  • When you say private domain, do you mean g-mail accounts for a company such that you can have options where accounts "within the company" can only view, access, edit, etc. Also, I made a copy of the file and the script didn't run until I went into the tab and ran each function. Honestly, the button idea seems the most appealing actually. The people I'm working with literally have zero coding knowledge, and sometimes stumble when doing simple sheet formulas. I think it would impress them actually. – mds93 Jul 26 '16 at 22:01
  • Yes, I am using "private domain" to mean any Google Apps Domain (business, education, non-profit). In the copied spreadsheet, the menu shows up a few seconds after opening the sheet. Selecting the menu's one option will start an authorization dialog, after which the trigger will operate. – Mogsdad Jul 26 '16 at 22:33
  • Thanks for the clarification! Unfortunately, the example sheet you linked wasn't working. I made a copy and the script wouldn't fire. I can see it say "Working..." at the top, but not changes in the sheets. – mds93 Jul 26 '16 at 22:36
  • Nevermind! I see what you mean now Mogsdad, thanks very much! – mds93 Jul 27 '16 at 15:43
1

From what I understood you don't need a script here, spreadsheet formulas will do the trick.
to import data from one spreadsheet to an other you can use the formula "importData" and put these data in a hiden sheet. then you can use "vlookup" formula on this import or even better a "filter" formula (try the filter formula you'll love it).

Harold
  • 3,297
  • 1
  • 18
  • 26
  • Hey Harold, first of all I appreciate the response! Secondly, I wish I could keep it restrained to sheet formulas, but because of the way the items price is figured and security issues, this is impossible for a couple reasons. Firstly, there are many steps taken to solve the items price - there are state taxes, item specific taxes, terms of goods, etc. Secondly, the user will need to be able to edit the sheet, and I'm largely concerned that someone will notice a hidden sheet, un-hide it, and then be privy to the information. – mds93 Jul 26 '16 at 14:41