How to Move a Row to Two Different Locations on a Different Google Sheets Tab Based off Two Different Criteria - if-statement

I would like to copy and paste a row from one google sheet tab titled "New Projects" to another google sheet tab titled "Project Tab" if column H says "Approved" and then clear the row that was copied. Additionally, I would like the destination of the copied row on the tab titled "Project Tab" to be conditional on column G on the tab "New Projects" before the copy and paste function is made. If column G says either "4" or "5" I would like to copy and past the row to row 8 on the tab called "Project Tab", else copy and paste the row to row 60 the tab called "Project Tab". So in summary: If column H says "Approved" in the "New Projects" tab check to see if column G has either a "4" or a "5". If it does move to row 8 on the tab called "Project Tab", else move to row 60. Below is a pictures of:
"New Projects" Tab
Top of "Project Tab" Tab
Bottom of "Project Tab" Tab"
I have created separate working function for sorting the rows once they are copied to the "Project Tab" tab. These functions are listed below in the picture:
Sorting Functions
The code below represents what I currently have. Right now it is copying the rows from "New Projects" tab and pasting in the "Project Tab" tab at row 8 regardless of what column G says on the "New Projects" tab. This is where I need help. How can I create a code that copies and pastes to a specific location based on column G, but the function does not run until it reads "Approved" in column H?
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
newprojectstoProjecttab(ss,s,r);
}
function newprojectstoProjecttab(ss,s,r){
if(s.getName() == "New Projects" && r.getColumn() == 8 && r.getValue() == "Approved") {
var row = r.getRow();
var targetSheet = ss.getSheetByName("Project Tab");
var target = targetSheet.getRange(60,1,1);
var prioritytarget = targetSheet.getRange(8,1,1);
if(r.getColumn() == 7 && r.getValue() == "4" || "5") {
s.getRange(row, 1, 1, 7).copyTo(prioritytarget);
var clearRange = s.getRange(row,1,1,8);
clearRange.clearContent();
} else {
s.getRange(row, 1, 1, 7).copyTo(target);
var clearRange = s.getRange(row,1,1,8);
clearRange.clearContent();
}
}
}
Any help would be much appreciated! If you need more information please let me know!

the first IF is sheet="New Products" and Column H = "Approved"
the second IF is the value of Column G: if (ColGValue == 4 || ColGValue == 5){
if true, then the target is column 8: var target = targetSheet.getRange(8,1,1)
if false, then the target is Column 60: var target = targetSheet.getRange(60,1,1)
thereafter, the commands are identical
copy the data to the target: s.getRange(editedRow, 1, 1, 7).copyTo(target)
erase the source: s.getRange(editedRow,1,1,8).clearContent()
var ss = spreadsheetApp.getActiveSpreadsheet()
var s = event.source.getActiveSheet()
// get the edited row, and the values of Column G and H
var editedRow = event.range.rowStart
var ColHValue = s.getRange(editedRow,8).getValue()
var colGValue = s.getRange(editedRow,7).getValue()
if(s.getName() == "New Projects" && ColHValue == "Approved") {
var targetSheet = ss.getSheetByName("Project Tab")
if (ColGValue == 4 || ColGValue == 5){
// if Column G =4 or 5, copy to row 8
var target = targetSheet.getRange(8,1,1)
}
else
{
// if Column G <> 4 or 5, then copy to row 60
var target = targetSheet.getRange(60,1,1)
}
s.getRange(editedRow, 1, 1, 7).copyTo(target)
s.getRange(editedRow,1,1,8).clearContent()
}

Related

How to subtract the value of another row depends on Category column in PowerBI

Here is the photo explanation
I want to show the NewLinePrice in "Other Service", the value will subtract the value of "Graphic Design", so the first column will show ‭1152245‬ and so on.
So far i've tried to defined the new column "NewLinePrice", the following is the formula but not work
NewLinePrice =
Var category = 'Group-dtl'[ProductCategoryName]
var graphic_line_price = CALCULATE(SUM('Group-dtl'[LinePrice]),FILTER('Group-dtl', 'Group-dtl'[ProductCategoryName] = "Graphic Design"))
var graphic_line_price_temp = IF(category = "Graphic Design", 'Group-dtl'[LinePrice], 0)
//var graphic_line_price = 1
Var pre_result = IF(category = "Other Service", 'Group-dtl'[LinePrice] - graphic_line_price, BLANK())
Var result = IF(pre_result > 0, pre_result, BLANK())
return result
Anyone have ideas how to do that?
I spend some time to find the answer for your question, at the end I discover that to achiever your outcome, you cannot perform the calculation within the original but to create a new table, the reason is unknown, however at least it is achievable, see my answer below and accept if help, appreciate my hardworking :)
This is my original table name [Sheet1]
First I create a new table based on the original table
Table =
ADDCOLUMNS(VALUES(Sheet1[Product Name]),
"Sales",CALCULATE(SUM(Sheet1[Amount]),
FILTER(ALL(Sheet1),Sheet1[Product Name]=EARLIER(Sheet1[Product Name]))))
From the new table, I add new column using the following formula to return different value only for "Other Service"
New Line1 =
Var ServiceValue = CALCULATE(MAX(Sheet1[Amount]),Sheet1[Product Name] = "Other Service")
Var graphicValue = CALCULATE(MAX(Sheet1[Amount]),Sheet1[Product Name] = "Graphic Design")
Var charge = ServiceValue - graphicValue
return
if('Table'[Product Name] = "Other Service", charge,'Table'[Sales])
Here is new table with updated value:

Replacing blank with zero for a mesure gives som unexpected extra rows

I have a little puzzle that annoys me in PowerBI/DAX. I'm not looking for workarounds - I am looking for an explanation of what is going on.
I've created some sample data to reconstruct the problem.
Here are my two sample tables written in DAX:
Events =
DATATABLE (
"Course", STRING,
"WeekNo", INTEGER,
"Name", STRING,
"Status", STRING,
{
{ "Python", 1, "Joe", "OnSite" },
{ "Python", 1, "Donald", "Video" },
{ "DAX", 2, "Joe", "OnSite" },
{ "DAX", 2, "Hillary", "Video" },
{ "DAX", 3, "Joe", "OnSite" },
{ "DAX", 3, "Hillary", "OnSite" },
{ "DAX", 3, "Donald", "OnSite" }
}
)
WeekNumbers =
DATATABLE ( "WeekNumber", INTEGER, { { 1 }, { 2 }, { 3 }, { 4 } } )
I have a table with events and another table with all weeknumbers and there is a (m:1) relation between them on the weekNo/weeknumber (I've given them different names to easily distinguish them in this example). I have a slicer in PowerBI on the weeknumber. And I have a table which shows aggregation and counts the participants based on the status with the following measures:
#OnSite = COUNTROWS(FILTER(events,Events[Status]="OnSite"))
#Video = COUNTROWS(FILTER(events,Events[Status]="Video"))
I visualize the two measures in a table together with the Course and the weekNo. With the slicer on weekNumber 3 there are nobody with status video so #video is blank. See screenshot.
Then I decided to create a new measure which should show a 0 instead of blank for the #video:
#VideoWithZero = VAR counter=COUNTROWS(FILTER(events,Events[Status]="Video"))
RETURN IF(ISBLANK(counter),0,counter)
I add the #VideoWithZero to the table and get a lot of extra rows in the table for the other weekNo's:
So my question is - Why do I get the extra rows for week 1 and 2 in the table? I would expect my filter on WeekNumber to filter them out.
The filter is being applied to the context of the query executed, and then the measures are calculated. Now the issue is that one of your measures is always returning a value (0), so regardless of your context it will always show a result, thus making it seem that it is ignoring the filter.
One way I tend to get implement this is by providing some additional context to when I might want to show 0 instead of blank. In your case it would be when one of the counts is not blank:
#OnSite =
VAR video = COUNTROWS(FILTER(events,Events[Status]="Video"))
VAR onsite = COUNTROWS(FILTER(events,Events[Status]="OnSite"))
RETURN IF(ISBLANK(video), onsite, onsite + 0) //+0 will force it not to be blank
#Video =
VAR video = COUNTROWS(FILTER(events,Events[Status]="Video"))
VAR onsite = COUNTROWS(FILTER(events,Events[Status]="OnSite"))
RETURN IF(ISBLANK(onsite), video, video + 0)
So on the OnSite measure it will check if there are Videos and if so, it adds +0 to the result of the OnSite count to force it not to be blank (and vice versa)
One other way could be to count total rows and subtract the ones different to the status you need:
#OnSite =
VAR total= COUNTROWS(Events[Status])
VAR notOnsite = COUNTROWS(FILTER(events,Events[Status]<>"OnSite"))
RETURN total - notOnsite
#Video =
VAR total= COUNTROWS(Events[Status])
VAR notVideo= COUNTROWS(FILTER(events,Events[Status]<>"Video"))
RETURN total - notVideo

How to highlight a column programmatically in AMCharts 4?

In AMCharts version 3, there is a demo showing how to highlight a particular column.
Is this possible using AMCharts version 4? For example, in the Simple Column demo, highlight the UK column based on its value (ie, where country = 'UK').
I tried modifying the example at https://stackoverflow.com/a/54358490/906814 but I can't get a handle on the columns in order to assess their values and then apply the active state highlight (JSFiddle).
// copied from https://stackoverflow.com/a/54358490/906814 but not working yet
var activeState = series.columns.template.states.create("active");
activeState.properties.fill = am4core.color("#E94F37");
series.columns.each(function(column) {
alert("column") // no alert is seen
column.setState("active");
column.isActive = true;
})
There are two approaches you can take.
1) Use an adapter on the column's fill and stroke and check the column value before modifying the color, e.g.
series.columns.template.adapter.add('fill', function(fill, target) {
if (target.dataItem && target.dataItem.categoryX == "UK") {
return "#ff0000";
}
return fill;
});
series.columns.template.adapter.add('stroke', function(stroke, target) {
if (target.dataItem && target.dataItem.categoryX == "UK") {
return "#ff0000";
}
return stroke;
})
Demo
2) Use a property field and set the stroke and fill from your data:
chart.data = [
// ...
{
"country": "UK",
"value": 1122,
"color": "#ff0000"
},
// ...
];
// ...
series.columns.template.propertyFields.fill = "color";
series.columns.template.propertyFields.stroke = "color";
Demo

Log date on cell text change in Google Sheets

I'm trying to log the date in a cell when a user tries to enter a text in another cell. To give you a clearer picture, I would like today's date to be logged in cell B1 when a user writes something in A1. I used this formula in B1:
=IF(A1="","",B1=TODAY())
but it doesn't work! Any idea how I can do this?
paste this in B1 cell:
=IF(A1=""; ; TODAY())
note: the date will change daily
_______________________________________________________
otherwise, it would be:
function onEdit(e) {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Sheet1" ) { // SHEET NAME
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { // COLUMN OF ENTRY
var nextCell = r.offset(0, 1); // OFFSET +1 COLUMN
var newDate = Utilities.formatDate(new Date(),
"GMT+8", "MM/dd/yyyy"); // TIMEZONE + DATE FORMAT
nextCell.setValue(newDate);
}}}

Apex Interactive Grid how to retrieve a specific record

I am retrieving my grid data using:
var ig$ = apex.region("myGrid1").widget(),
view = ig$.interactiveGrid("getCurrentView");
Now I want to check for a specific record based on 2 columns: id1 and id2 where id1 = 1 and id2 = 7
How can I do that with javascript?
You can iterate for each record like this:
//"myGrid1" should be the static id of the IG region
var widget = apex.region('myGrid1').widget();
var grid = widget.interactiveGrid('getViews','grid');
var model = grid.model;
var results = [];
model.forEach(function(r) {
var record = r;
//the name of the columns should be ID1 and ID2, if not
//make the necessary changes using "_" to represent "space"
var value1 = model.getValue(record,'ID1');
var value2 = model.getValue(record,'ID2');
if(value1 == 1 && value2 == 7) {
results.push(record);
}
})
console.log(results);
To test this code, execute it on console.
To start the console on chrome just press F12
good luck.