Linking a Macro TBD - google-apps-script

Can you insert a Macro command into a link?
I see you can create a button via "insert drawing" and additional script details but I like to click a link and it performs my macro.

You could create a web app and use it's end point url to call any of your functions by name through the use of the parameters in the query string.

You could use the onSelectionChange(event) trigger. If you move the cursor and click on a cell it will tell you which cell was clicked and you could associate another function with that cell. If you click in the same cell it won't do anything.
However what I find is, although it is a simple trigger, you have to close and reopen the Spreadsheet for it to become active.
function onSelectionChange(event) {
// event = {"range":{"columnEnd":3,"columnStart":3,"rowEnd":1,"rowStart":1},"authMode":"LIMITED","source":{},"user":{"email":"xxxx#gmail.com","nickname":"xxxx"}}
Logger.log(JSON.stringify(event));
}

Related

Best way to create and manage someone's triggers (GAS)

I wrote some GAS code for a person and it required a trigger.
I created this line and asked the person to call it to set up his trigger (as it is his spreadsheet):
ScriptApp.newTrigger("myFunction").timeBased().everyDays(1).inTimezone("Australia/Brisbane").atHour(5).create()
But then, I cannot see his trigger.
To see if a trigger is there I used:
Logger.log(ScriptApp.getProjectTriggers())
It returned an empty array so looks like the trigger is gone but I don't see it physically...
So, if it's a client, it is not cool to ask his "please set up your trigger" and then not be able to see it and then ask to check if it's there.
What's the best way to set up and manage triggers for clients?
EDIT: As stated in the accepted answer, It seems other user's triggers are viewable manually in the dashboard.
Other user's triggers are not available for you to view programmatically/manually for security reasons.
I created this line and asked the person to call it to set up his trigger (as it is his spreadsheet):
You must explicitly create a menu , so that the user may click on it to create the trigger:
If the user sets up a trigger through menu options, You can set the id of trigger to properties service like this.
function createTrigger(){ //to be linked to a "Create Trigger" menu
// Creates an edit trigger for a spreadsheet identified by ID.
var tid = ScriptApp.newTrigger('myFunion')
.forSpreadsheet('1234567890abcdefghijklmnopqc3')
.onEdit()
.create()
.getUniqueId();
//set id of trigger in properties
PropertiesService
.getScriptProperties() //if you want public access to trigger(If not use getUserProperties)
.setProperty(
Session.getEffectiveUser().getEmail() ||
('user1 ' + Math.random().toFixed(3)),
tid
);
//Inform user: trigger created successfully.
SpreadsheetApp.getActive().toast('Trigger created successfully with id: ' + tid);
//#see https://script.google.com/home/triggers
}
To see his triggers, open the editor and choose "Current Project Triggers" from the Edit menu.
On the trigger page, click the "x" on the "Owned by me" filter. Then you shold see all the triggers for the project and know if it is set up. It won't tell you who owns it, I don't think.
You cannot "manage" his triggers from there, just see them. There is no way to mess with other peoples triggers (a much debated topic!) but you can change the name of the function to break a trigger.

Passing in a Parameter in Google Spreadsheet Script from Script Assignment

I'm trying to make my own button in a Google Spreadsheet, and I have a script called incrementContents that takes in a parameter (a cell location, eg 'B3') and increments that location by one.
My question is this- is there a way to pass in a parameter to this function via the same text box where I assign the script to the button (right click -> assign script)? Something like;
incrementContents('B3')
or
incrementContents, B3
I'm not sure of the syntax, and knowing how to do this would be very helpful.
You could do a couple of things -
Ask the user for the input using Browser.inputBox
Read cell's value from the script itself (if the cell reference is always constant) using an example like SpreadsheetApp.getActiveSheet().getRange("A1")
The way I am trying to work around this problem is by having the user edit a cell instead of adding a button to the cell. (Eg. typing "ok" in the cell.) This triggers the onEdit() function or a custom function that is passed the event parameter that can be used to get data from the sheet relative to the cell where the "ok" was entered.

Google Spreadsheet Script not Updating Dynamic Info

So, i made a spreadsheet for Minecraft and 'Feed The Beast' a modpack for the game. My spreadsheet lists all the mods that are updated for the latest version of Minecraft. Recently i made a script to go out and parse pages for mod version numbers. They change hourly-daily and i rather not do it manually.
function GetVersion(url, slice, num1, num2) {
var r = UrlFetchApp.fetch(url).getContentText();
var version = r.slice(r.indexOf(slice)).slice(num1, num2);
return version;
}
In one of the cells Ill have the following
=GetVersion("http://files.minecraftforge.net", "Build 7.", 12, 15)
Now this works fine and gets me the version number im looking for. The problem is, when an update happens and a new version comes out, the parser doesn't reflect this even if i close the window and reopen the spreadsheet or reload or whatever else! If I change the above slightly, like change the 15 to 16, it will refresh, but then will be stuck there again until i manually change it again.
How do I get it so it at least refreshed when i reload the sheet?
Edit: Alright first I tried to go into reosources and triggers and make a trigger very min and everytime the doc is opened. This didn work..............
Then I tried to get clever. Noticing that the formula reevaluates whenever I change it, i surmised that the formula itself or the cell parameters needs to change in order for this to happen. So I pass a dummy parameter though in the formula and change that parameter to update the cell.
But that's annoying and make me have to edit (or press a button) just to get shit to refresh.
So i got a brainwave and Im now passing GoogleClock() in the dummy parameter. All cells now update on their own every 60 seconds. yay
Is there a better way to do this?
Using googleclock as a param is the best u can do if u want to use a custom cel formula.
Instead call an update function from a menu item and/or onOpen / time trigger.
Aside from the workaround of passing GoogleClock() as a parameter (which I agree is the best you are going to do with a custom function), the other option is to do away with a custom function, and use GAS to write the result directly to a cell, eg:
SpreadsheetApp.getActiveSpreadsheet().getRange('Sheet1!A1').setValue(version);
Then in Resources, Current project's triggers, you can set both an "on edit" and time-driven trigger (to be clear: these triggers won't work on a custom function called from a spreadsheet cell; all the "work" must be done by the script itself).

Make the JTable cell ready to be overwritten

I have a button which inserts a single column row into a JTable with the text:
"New notes: ". I want this text ready to be overwritten as soon as the user starts typing on this cell (rightafter the button-press). How to make the JTable's cell ready to be overwritten? How can this be done?
Thank you!
You will need to use a custom TableCellEditor, when getTableCellEditorComponent is called, if the value is null, you would seed the editor with your prompt and call the selectAll method (assuming you are using some kind of JTextComponent for the editor).
This may require you to call selectAll within a SwingUtilities.invokeLater call.
Alternatively, you could have a look at this prompt API

Change the query being referenced by a function, depending on context

I have a custom function in Access 2007 that hinges on grabbing data out of a specific query. It opens Outlook, creates a new email and populates the fields with specific addresses and data taken from the query ("DecisionEmail").
Now I want to make a different query ("RequestEmail") to populate the email with that data. So all I have to do is change this line:
Set MailList = db.OpenRecordset("DecisionEmail")
This is my desired result: If the user is on Form_Decision and clicks the button "Send email", "DecisionEmail" will get plugged into the function and that data will appear in the email. If the user on Form_SendRequest and clicks the button "Send email", "RequestEmail" will get plugged in instead.
My last resort would be to make a new function and use the Conditions field in the Macro interface to choose between them.
I have a vague notion of setting the query names as variables and using an If statement.
Why not save the macro as code, then you can edit away to achieve say:
Function CustomEmail(NameOfQuery As String)
CurrentDB.Openrecordset(NameOfQuery)
End Function
Then, on each form in the desired event:
CustomEmail "DecisionEmail"