Google Spreadsheet Cycler - javascript

First of all, I know I've posted something similar to this previously, but i got one response that I couldn't understand and that was it. So, I am trying to make an app script for google sheets to be able to increase the value of the number in a specific cell every weekday. I already know how to set up the time-based trigger for the timing, so all I need is the actual code. I know very minimal about coding, and this is the best I could come up with:
function onEdit(e) {
var range = e.range
var sheet = e.range.getSheet();
sheet.getRange(range.getRow(), 1).setValue(e.(e + 1));
}
When I try to run it, it says:
TypeError: Cannot read property "range" from undefined.(line 2,file"")
Any ideas on how to fix this would be greatly appreciated! But please, please put it in terms that a noob can understand!

The code you have now is on the right track but with a bit of a misunderstanding.
The onEdit trigger IS a trigger, but it is not a timebased one. onEdit is called every time a cell is edited in the spreadsheet. If you are calling this function from a time-based trigger, it will not pass an event object (e) like it would if it were triggered by an edit, which is why you are getting your error.
Instead, modify your time based trigger to call a different function that doesn't depend on an event object.
My understanding is that it is a static cell that you are incrementing (what I mean by this is that it will always be in the same place) so my suggestion is to grab the spreadsheet, grab the appropriate sheet, grab the cell, and edit its contents, similar to what you are already doing but more manual. In code, it would look like this :
function incrementCell(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName');
var value = sheet.getRange('B2');
value.setValue(value.getValue() + 1);
}
Simply substitute your sheets name and your cells A1 Notation location into the code and point your time-drive trigger to this function and it should work for you.

Related

Is there a way to get the "parent" Spreadsheet which triggered the code?

I have a script that executes a code, i've attached it to some buttons in differents sheets and inside this code i need to get the sheet name that contains the buttom that triggered the script. Is there a way to get it?
I've coded this so far:
let sht = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Escopo - Financeiro");
var nomeDoc = sht.getRange("A2").getValue();
var modulo = sht.getRange("B1").getValue();
So i need to change the .getSheetByName("Escopo - Financeiro") code to fit my needs.
I was wondering if i could use some certain of "this" or "parent" statement, but i havent found it.
So since i've bounded my script to a button which is into an active spreadsheet, replacing the SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Escopo - Financeiro"); part of the code to SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); solved the problem.

How can I create a script for a popup button on cell value change in Google Sheets?

I am still basic level in javascript and have spent hours searching and testing scripts but none are giving me results. I need a script that will create a popup warning upon cell change reaching "X". This is roughly where I finally stopped trying:
function onEdit(e){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var range=e.range;
var column=range.getColumn();
var row=range.getRow();
var status=range.getValue();
var sh=SpreadsheetApp.getUi();
if(column===45 && row>1){
range.setAlert
var response=sh.alert('Warning!','Employee has used Maximum allowed
hours, do you wish to proceed?',sh.ButtonSet.YES_NO);
}
}
I keep receiving an error about the range, if I input rows/columns or the actual like C2:C10 I get a different error. I am not sure what I am doing wrong. Also, is there a way to ensure this populates for that one cell, that instance, and not each time an instance occurs in the same column under the same criteria? Basically, the cells in one column all need the script to search, just not repeat by accident.
Any help is appreciated.

sharepoint Online/365 Jquery, Set Lookup Column Not working e.i ($("select[Title='Column']")

I've been doing some work on Sharepoint Online/365 and have got stuck trying to set the value of a lookup or choice column in NewForm.aspx
Narrowed my problem down to not being able to set lookup/Choice Columns
I have simplified a code on my page down to
//Phase being a Choice Column & Closure a case sensitive valid option
$("select[title='Phase']").val("Closure");
//ProjectName being a Lookup Column & Test2 a case sensitive valid entry in the list
$("select[title='ProjectName']").val("Test2");
I have no problem setting text fields as the following code works fine on a text field I created
$("input[title='TextField']").val("Test2");
I have used the following Jquery libraries 1.7.2/min.js and 1.11.3
Not sure whether you used _spBodyOnLoadFunctionNames.push() method for your logics. Usually you have to wait all SharePoint DOM objects are loaded and then to execute your jQuery code. If you used SharePoint JavaScript library, you probably needs to call ExecuteOrDelayUntilScriptLoaded() to make sure your call is called after curtain SharePoint .js files are loaded.
First Thanks "Verona Chen" and " DIEGO CARRASCAL" because of who i've learnt a few other tricks in SharePoint which will help with other projects.
My original script before the question was trying to use a query string to populate a field in newform.aspx (which i have done on sharepoint 2013 with code i have found here on)
Unforuntaly with sharepoint online/365 This code was no longer working.
This code has fixed my issue (though it does change how a few previous pages are constructed)
Appologies if this doesn't directly answer the above question (as this was me trying to breakdown the overall issue i was having into something simpler and easier to address based on me narrowing down the issue in my original code) however as I am now up and running, it seems only polite to post the outcome.
Prior to code, i have a projects list with a "ProjectName" field. I was sending the field name into a URL and querystring to get mylist/newform.aspx?ProjectName=Test2
I was then trying to pull that Test2 into the lookup field (liked to the project list) "ProjectName" in the list "MyList"
But even when loading the function for my old script with _spBodyOnLoadFunctionNames.push() it wasn't working.
After playing with this for a while and after some looking around i found this peice of code
<script type="text/javascript">
(function () {
var ctx = {};
ctx.Templates = {};
ctx.Templates.Fields = {
'ProjectName': {
'NewForm': renderTaskCategory
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();
function renderTaskCategory(ctx) {
//extract cat parameter from a query string
var GetProjID = GetUrlKeyValue('ProjID');
//set lookup field value
ctx.CurrentFieldValue = GetProjID;
//default template for rendering Lookup field control
return SPFieldLookup_Edit(ctx);
}
</script>
This means that i have to change my previous url builds to create mylist/newform.aspx?ProjID=2
This script then finds item ID 2 (which happens to be test2 in this case) and puts the title of item 2 in my lookup field ProjectName
Thanks again to those that helped earlier.
And apologies again if this doesn't directly answer the question i originally asked.

Slickgrid - how to get the value of edited cells?

I'd like to make my code be able to update database table immediately when the cell is changed. However, I can get the value of the changed cell whereas not the data of those of next cell.
Methods I have tried already without success:
grid.onCellChange.subscribe(
function (e, args) {
//alert(data[args.row][grid.getColumns()[args.cell].field]);
//alert(grid.getColumns()[args.cell].name);
//alert(args);
<%
updateDatabase("UPDATE table1 "+
"SET "+
" col1="+data[args.row][grid.getColumns()[args.cell].field]+" "+
"WHERE "+
" col2="+???)")
%>
}
);
How to get the value of the next column that I can use in the where clause ?
Addressing your problem:
If you check out the official SlickGrid Editing demo, you can try the following to get a basic understanding how grid editing works. Open your JS debugger and subscribe to the grids onCellChange event using the following command:
grid.onCellChange.subscribe(
function (e,args) {
console.log('row: ' + args.row + ' cell: ' + args.cell)
});
The grid's onCellChange events args argument object contains two properties:
row
cell
Now edit any of the cells and leave the just edited cell. It's important since this event will only fire on blur event.
In the console log you will see for instance:
row: 6 cell: 1
Addressing your data object:
The only way I found how could you address your data source is in a hackish way only using eval(). For further information about object property accessing you can check this answer.
A bit from the Javascript jargon:
The actual eval() command to address your data object with the given fieldname or property name is:
eval('data[args.row]'+'.'+columns[args.cell].field)
Simplified it will evaluate the JavaScript code represented as a string, in this SlickGrid Editing example it will become as if you would write:
data[6].title
So the proof of concept code is:
grid.onCellChange.subscribe(
function (e,args) {
console.log(eval('data[args.row]'+'.'+columns[args.cell].field))
});
This will return you the value of the current edited cell. For instance changing args.cell+1 will return you the next cell value from the data source of your grid. Of course you should always check the columns.length property to keep the column index key inside of array bounds.
Regarding your idea about the updateDatabase call: why do you want to update the database after every time this event gets fired? I suggest you to take a look at the official Composite editor demo which has an edit form.
The other thing I would strongly advise is that you not write server side or code behind code directly into the javascript code block. For example, you could make it use an ajax post.
The reason I recommend this is because then you are going to have separated functions with separated responsibilities. Just by adhering to this principle in itself could lead you to have a cleaner code, also you only give one reason to change. Also it may help testing your functions much easier.

How do I Get and Set a field in a Onedrive embedded Excel sheet?

In JS, how do I Get or Set a field in a Onedrive embedded Excel sheet?
I'm really just looking for the any simple solution for it.
History:
At present, I'm using the Javascript setup for adding the project, as found here.
I'm also using this script to get the sheet, once it is loaded.
var a = Ewa.EwaControl.getInstances().getItem(0);
var b = a.getActiveWorkbook();
var c = b.getActiveSheet();
But with the active sheet, I don't know how to get the cells or set them.
If you know how to finish the code I have, great. If you know another solution, that would also be fine.
Thanks.
To Get/Set the cell data, use the Async call from c (the selected range)
GET:
https://msdn.microsoft.com/en-us/library/office/ee588949.aspx
SET:
https://msdn.microsoft.com/en-us/library/office/ee588957.aspx

Categories

Resources