Google Spreadsheets tickbox increment and time-driven reset - checkbox

I would like to use tickbox function on spreadsheets; when its ticked at the end of the day defined cell values will increment and tickbox will reset.
function resetCells() {
var ss = SpreadsheetApp.getActive()
.getSheetByName('Lordaeron');
var cells = ['B2:B1000']
.map(function (c) {
ss.getRange(c)
.setValue("FALSE");
});
}
This resets checkbox with time trigger I need to add conditional increment. If B columb value is TRUE C columb value will increment in the same row.
Example:
Initial Sheet >>
Checkbox | Value 1 | Value 2 |
-------------------------------
X | 1 | 0 |
V | 2 | 0 |
X | 3 | 1 |
V | 1 | 2 |
X | 2 | 3 |
One day later [Desired Result] >>
Checkbox | Value 1 | Value 2 |
-------------------------------
X | 1 | 0 |
X | 3 | 1 |
X | 3 | 1 |
X | 2 | 3 |
X | 2 | 3 |
https://docs.google.com/spreadsheets/d/1khPC5r2p0b1srsEGka3fl-GAl6nHACaVR0Cf31cqA1o/edit?usp=sharing here is example base sheet.
Thanks in advance
==============================================
Additinally I asked for how to add upper limit as 100 to not to reach certain number.
Thanks to Tanaike I fixed issue by this code snippet:
function resetDaily() {
var ss = SpreadsheetApp.getActive().getSheetByName('Sheet');
var range = ss.getDataRange().offset(1, 0, ss.getLastRow() - 1);
var values = range.getValues().map(function(e) {
if (Number(e[8]) <= 94) {
return e[0] ? [false, e[1], Number(e[2]) + 6, Number(e[3]) + 1] : e;
} else {
return e[0] ? [false, e[1], 100, Number(e[3]) + 1] : e;
}
});
range.setValues(values);
}

resetCells() is installed as the time trigger for doing If tickbox is checked at the end of the day.. This is done by yourself.
When resetCells() is run, it checks all checkboxes of column "A".
If the checkbox of a row is false, do nothing for the row.
If the checkbox of a row is true, for the row, it add +6 to the value of column "C" and add +1 to the value of column "D". And then, it changes the value of column "A" of the row to false.
If my understanding your situation and what you want is correct, how about this modification? I think that there are several answers for your situation. So please think of this as one of them.
Modification points:
In your script, all values of column "A" change to false. But the original values cannot be retrieved. So there are no materials for modifying column "C" and "D".
Flow of this modified script is as follows.
Retrieve all values of the data range.
Modify the values.
Overwrite the sheet using the modified values.
Modified script:
Please modify your script as follows.
function resetCells() {
var ss = SpreadsheetApp.getActive().getSheetByName('Sheet');
var range = ss.getDataRange().offset(1, 0, ss.getLastRow() - 1);
var values = range.getValues().map(function(e) {
return e[0] ? [false, e[1], Number(e[2]) + 6, Number(e[3]) + 1] : e;
});
range.setValues(values);
}
Note:
If there are often the situation that all values of column "A" are false, I think that the process cost can be reduced by including the script for checking the values of column "A".
Added:
In this update, the script can change the value as a boundary when the value of column "C" is 100.
function resetCells() {
var ss = SpreadsheetApp.getActive().getSheetByName('Sheet');
var range = ss.getDataRange().offset(1, 0, ss.getLastRow() - 1);
var values = range.getValues().map(function(e) {
if (Number(e[2]) < 100) { // or <= 100
return e[0] ? [false, e[1], Number(e[2]) + 6, Number(e[3]) + 1] : e;
} else {
// do something
}
});
range.setValues(values);
}

Related

Comparing rows in spark dataframe to obtain a new column

I'm a beginner in spark and I'm dealing with a large dataset (over 1.5 Million rows and 2 columns). I have to evaluate the Cosine Similarity of the field "features" beetween each row. The main problem is this iteration beetween the rows and finding an efficient and rapid method. I will have to use this method with another dataset of 42.5 Million rows and it would be a big computational problem if I won't find the most efficient way of doing it.
| post_id | features |
| -------- | -------- |
| Bkeur23 |[cat,dog,person] |
| Ksur312kd |[wine,snow,police] |
| BkGrtTeu3 |[] |
| Fwd2kd |[person,snow,cat] |
I've created an algorithm that evaluates this cosine similarity beetween each element of the i-th and j-th row but i've tried using lists or creating a spark DF / RDD for each result and merging them using the" union" function.
The function I've used to evaluate the cosineSimilarity is the following. It takes 2 lists in input ( the lists of the i-th and j-th rows) and returns the maximum value of the cosine similarity between each couple of elements in the lists. But this is not the problem.
def cosineSim(lista1,lista2,embed):
#embed = hub.KerasLayer(os.getcwd())
eps=sys.float_info.epsilon
if((lista1 is not None) and (lista2 is not None)):
if((len(lista1)>0) and (len(lista2)>0)):
risultati={}
for a in lista1:
tem = a
x = tf.constant([tem])
embeddings = embed(x)
x = np.asarray(embeddings)
x1 = x[0].tolist()
for b in lista2:
tem = b
x = tf.constant([tem])
embeddings = embed(x)
x = np.asarray(embeddings)
x2 = x[0].tolist()
sum = 0
suma1 = 0
sumb1 = 0
for i,j in zip(x1, x2):
suma1 += i * i
sumb1 += j*j
sum += i*j
cosine_sim = sum / ((sqrt(suma1))*(sqrt(sumb1))+eps)
risultati[a+'-'+b]=cosine_sim
cosine_sim=0
risultati=max(risultati.values())
return risultati
The function I'm using to iterate over the rows is the following one:
def iterazione(df,numero,embed):
a=1
k=1
emp_RDD = spark.sparkContext.emptyRDD()
columns1= StructType([StructField('Source', StringType(), False),
StructField('Destination', StringType(), False),
StructField('CosinSim',FloatType(),False)])
first_df = spark.createDataFrame(data=emp_RDD,
schema=columns1)
for i in df:
for j in islice(df, a, None):
r=cosineSim(i[1],j[1],embed)
if(r>0.45):
z=spark.createDataFrame(data=[(i[0],j[0],r)],schema=columns1)
first_df=first_df.union(z)
k=k+1
if(k==numero):
k=a+1
a=a+1
return first_df
The output I desire is something like this:
| Source | Dest | CosinSim |
| -------- | ---- | ------ |
| Bkeur23 | Ksur312kd | 0.93 |
| Bkeur23 | Fwd2kd | 0.673 |
| Ksur312kd | Fwd2kd | 0.76 |
But there is a problem in my "iterazione" function.
I ask you to help me finding the best way to iterate all over this rows. I was thinking also about copying the column "features" as "features2" and applying my function using WithColumn but I don't know how to do it and if it will work. I want to know if there's some method to do it directly in a spark dataframe, avoiding the creation of other datasets and merging them later, or if you know some method more rapid and efficient. Thank you!

Excel - VLOOKUP to return each result in an array, not just the first

I am currently working between two workbooks.
In Workbook A I have the following data.
A ... D E F ... N
1.| ID | Name | Desc | Prod | Country|
2.| 12345 | Apple| Fruit| 10| US|
3.| 12346 | Celery| Veg| 150| US|
4.| 12347 | Mint| Herb| 25| FR|
I have been using the following formula from AHC in Workbook B, the aim is to perform a VLOOKUP which grabs all the ID's but only if the Country = "US".
=VLOOKUP("US", CHOOSE({2,1},Workbook A.xlsx!Table1[ID], Workbook A.xlsx!Table1[Country]), 2, FALSE)
This formula works well, however, my problem comes because the formula will only ever return the first instance in the array. For example, if I include this formula in Workbook B, Col A it will look like this:
A
1.|ID of US|
2.| 12345 |
3.| 12345 |
4.| 12345 |
5.| 12345 |
6.| 12345 |
7.| 12345 |
How would I make this formula work so that it returns each ID which matches "US", not just the first occurrence of a match?
In B2 put this formula:
You might need to adjust the rows of the ranges (I went till 100).
={ISERROR(INDEX(D$2:D$100,SMALL(IF(N$2:N$100=$A2,ROW(D1)),ROW(N1))),"")}
NOTE:
Step 1) Insert the formula only in Cell B2 without the {}
Step 2) Once the formula is inserted mark the entire formula and press Ctrl + Shift + Enter so the formula will get the {}
Step 3) Drag it down the rows as far as you need it to get the list.

Find product of integers at interval of X and update value at position 'i' in an array for N queries

I have given an array of integers of length up to 10^5 & I want to do following operation on array.
1-> Update value of array at any position i . (1 <= i <= n)
2-> Get products of number at indexes 0, X, 2X, 3X, 4X.... (J * X <= n)
Number of operation will be up to 10^5.
Is there any log n approach to answer query and update values.
(Original thought is to use Segment Tree but I think that it is not needed...)
Let N = 10^5, A:= original array of size N
We use 0-based notation when we saying indexing below
Make a new array B of integers which of length up to M = NlgN :
First integer is equal to A[0];
Next N integers is of index 1,2,3...N of A; I call it group 1
Next N/2 integers is of index 2,4,6....; I call it group 2
Next N/3 integers 3,6,9.... I call it group 3
Here is an example of visualized B:
B = [A[0] | A[1], A[2], A[3], A[4] | A[2], A[4] | A[3] | A[4]]
I think the original thoughts can be used without even using Segment Tree..
(It is overkill when you think for operation 2, we always will query specific range on B instead of any range, i.e. we do not need that much flexibility and complexity to maintain the data structure)
You can create the new array B described above, also create another array C of length M, C[i] := products of Group i
For operation 1 simply use O(# factors of i) to see which Group(s) you need to update, and update the values in both B and C (i.e. C[x]/old B[y] *new B[y])
For operation 2 just output corresponding C[i]
Not sure if I was wrong but this should be even faster and should pass the judge, if the original idea is correct but got TLE
As OP has added a new condition: for operation 2, we need to multiply A[0] as well, so we can special handle it. Here is my thought:
Just declare a new variable z = A[0], for operation 1, if it is updating index 0, update this variable; for operation 2, query using the same method above, and multiply by z afterwards.
I have updated my answer so now I simply use the first element of B to represent A[0]
Example
A = {1,4,6,2,8,7}
B = {1 | 4,6,2,8,7 | 6,8 | 2 | 8 | 7 } // O(N lg N)
C = {1 | 2688 | 48 | 2 | 8 | 7 } // O (Nlg N)
factorization for all possible index X (X is the index, so <= N) // O(N*sqrt(N))
opeartion 1:
update A[4] to 5: factors = 1,2,4 // Number of factors of index, ~ O(sqrt(N))
which means update Group 1,2,4 i.e. the corresponding elements in B & C
to locate the corresponding elements in B & C maybe a bit tricky,
but that should not increase the complexity
B = {1 | 4,6,2,5,7 | 6,5 | 2 | 5 | 7 } // O(sqrt(N))
C = {1 | 2688 | 48/8*5 | 2 | 8/8*5 | 7 } // O(sqrt(N))
update A[0] to 2:
B = {2 | 4,6,2,5,7 | 6,5 | 2 | 5 | 7 } // O(1)
C = {2 | 2688/8*5 | 48/8*5 | 2 | 8/8*5 | 7 } // O(1)
// Now A is actually {2,4,6,2,5,7}
operation 2:
X = 3
C[3] * C[0] = 2*2 = 4 // O(1)
X = 2
C[2] * C[0] = 30*2 = 60 // O(1)

Assigning through random generated indexes

I'm trying to write a function that assign N elements to M players. Here's what I wrote:
void assignElements(Player *p, Tab *t, int n) {
int i = 0, nRand, flagElements = 0;
do {
do {
nRand = MINRANDT + rand() % (MAXRANDT - MINRANDT + 1);
} while(t[nRand].type != Terrain && t[nRand].idProp != -1);
if (i == n) {
i = 0; //this makes "i" reset when it reaches the number of players
}
t[nRand].idProp = i;
p[i].numTerrains++;
i++;
flagElements++;
} while (flagElements != NELEMENTS);
}
So, if I try to run this function, it's not respecting the condition of the second while (maybe the problem is the condition.): in fact, it also assign to t elements that are not of type Terrain (this is an enum). The condition to do the actions under the nRand do / while, is that nRand have to be an index of a t element that is of type Terrain, and that its idProp is -1 (this mean it has not been assigned yet). Hope anyone can help. :)
t[nRand].type != Terrain | t[nRand].idProp != -1 | &&
-------------------------+-----------------------+---
0 | 0 | 0
0 | 1 | 0
1 | 0 | 0
1 | 1 | 1
Meaning that the loop will exit for 3 conditions and repeat only when both sub-conditions are met
Try ||
t[nRand].type != Terrain | t[nRand].idProp != -1 | ||
-------------------------+-----------------------+---
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1

please explain the output

#include<stdio.h>
#include<conio.h>
int t=8;
int dok(int);
int doky(int);
int main()
{
int clrscr();
int x,y;
int s=2;
s*=3;
x=dok(s);
y=doky(s);
printf("%d%d%d",s,y,x);
getch();
return 0;
}
int dok(int a)
{
a+=-5;
t-=4;
return(a+t);
}
int doky(int a)
{
a=1;
t+=a;
return(a+t);
}
Answer to above code: 665
I understand why s=6, x=1+4=5 (a=6-5=1,t=8-4=4)... Please tell me how y comes as 6, I thought y would be 1+4=5 (a=1, t=4)
Thanks, please help me.
tell me how y comes as 6 ...
Call to dok function modifies t to 4.
int doky(int a)
{
a=1;
t+=a; // Previously t is 4 because of t-=4 in earlier function call
// t = 4+1 = 5
return(a+t); // 1+5 = 6 retured
}
first t increases by a and then sum of a and t is returned
so, t was 4. then operator t += a is executed and t becomes 5.
and a+t == 1+5 == 6 is returned
The value of t is changed to 4 with the dok function, and the doky function increments that value by 1 (the value in a). Sum that (5 so far) to the value of a again (set to 1), and that's 4+1+1 = 6.
//t is 4, the value of a is irrelevant since it changes on the next instruction.
a=1;
t+=a; // t is now 5
return(a+t); // 1+5 = 6
y = a + t = a + t + a = 1 + 4 + 1 = 6 :)
Just do it with pencil and paper ...
| t | x | y | s | a |
-----------------+---+---+---+---+---+
before main | 8 |#NA|#NA|#NA|#NA|
before x=dok(s) | 8 | ? | ? | 6 |#NA|
inside dok | 4 |#NA|#NA|#NA| 1 |
after dok | 4 | 5 | ? | 6 |#NA|
before y=doky(s) | 4 | 5 | ? | 6 |#NA|
inside doky | 5 |#NA|#NA|#NA| 1 |
after doky | 5 | 5 | 6 | 6 |#NA|

Resources