Quota script for a roster - google-apps-script

I'm trying to make a script that reads a specific column(B), checking each cell for "is this value < 120?".
If the value is < 120, I want the script to add a value of "1" to the corresponding cell in a different column(E).
This is what I've come up with so far, but it's not working and I can't figure out why.
function quota1() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Roster");
var workingCell = sheet.getRange("B:B").getValue();
if(workingCell < 120){
sheet.getrange("E:E").add(1);
}
}
Picture of roster for better understanding.

If col2 less 120 increment column 5 by 1
function quota1() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Roster");
const vs = sh.getRange(1,1,sh.getLastRow(),5).getValues();//get all data
let vo = vs.map((r,i) =>{
if(r[1] < 120) {
return [r[4] + 1];//add one
} else {
return [r[4]];//no change
}
});
//Logger.log(JSON.stringify(vo));//easier see the column
sh.getRange(1,5,vo.length,vo[0].length).setValues(vo)
}
Before:
105
5
106
6
107
7
108
8
109
9
110
10
111
11
112
12
113
13
114
14
115
15
116
16
117
17
118
18
119
19
120
20
121
21
122
22
123
23
124
24
After:
105
6
106
7
107
8
108
9
109
10
110
11
111
12
112
13
113
14
114
15
115
16
116
17
117
18
118
19
119
20
120
20
121
21
122
22
123
23
124
24

Related

Sorting columns left to right in Google Sheets ascending order with app script

I'm trying to sort columns from left to right based on dates, here is an example of the issue that I'm facing:
https://docs.google.com/spreadsheets/d/1CuDW-VRZxrwXXjyBj4BeUleMFqL8DUQrW3sku6WjMh0/edit?usp=sharing
I'm sorting from column E to N based on the dates in row 6. The script that I'm currently using works okay as far as cell E6 has a date and there is no empty columns in between the full ones, otherwise the script won't work.
Here's the script that I'm using:
function sortLToR() {
//Defining the spreadsheet variables and setting ranges
var sheet = SpreadsheetApp.getActive().getSheetByName("Sort");
var range3 = sheet.getRange(5, 5, 88,sheet.getLastColumn()-4)
var range = sheet.getRange(5, 5, 88,sheet.getLastColumn()-4).getValues();
Logger.log(sheet.getLastColumn())
//Defining a blank array that can hold the result
var trans = [];
//transpose the data stored in range variable
for(var column = 0; column < range[0].length; column++){
trans[column] = [];
for(var row = 0; row < range.length; row++){
trans[column][row] = range[row][column];
}
}
function sortByDate(a, b) {
return new Date(b[1]).getTime() - new Date(a[1]).getTime();
}
var range2 = trans.sort(sortByDate);
var trans2 = [];
//transpose the data stored in range variable
for(var column = 0; column < range2[0].length; column++){
trans2[column] = [];
for(var row = 0; row < range2.length; row++){
trans2[column][row] = range2[row][column];
}
}
range3.setValues(trans2);
}
Any ideas how to fix this?
Thanks
Sort Columns by date and move blank columns to right so that data always starts in Column E
This version puts the blank columns to the right and always starts at E
function sortLToR() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const rg = sh.getRange(5, 5, sh.getLastRow() -4, sh.getLastColumn() - 4)
const vs = rg.getValues();
let b = transpose(vs);
b.sort((a,b) => {
if(a[1] && b[1]) {
return new Date(a[1]).valueOf() - new Date(b[1]).valueOf();
} else if(a[1] && !b[1]){
return -1;
} else if(!a[1] && b[1]) {
return +1
}
});
let c = transpose(b)
rg.setValues(c);
}
Demo:
Before Sort:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
COL2
COL3
COL4
COL5
COL6
COL7
COL8
COL9
COL10
5/19/2022
5/18/2022
5/17/2022
5/16/2022
5/15/2022
5/14/2022
5/13/2022
5/12/2022
5/11/2022
3
4
5
6
7
8
9
10
11
4
5
6
7
8
9
10
11
12
5
6
7
8
9
10
11
12
13
6
7
8
9
10
11
12
13
14
7
8
9
10
11
12
13
14
15
8
9
10
11
12
13
14
15
16
9
10
11
12
13
14
15
16
17
10
11
12
13
14
15
16
17
18
11
12
13
14
15
16
17
18
19
12
13
14
15
16
17
18
19
20
13
14
15
16
17
18
19
20
21
14
15
16
17
18
19
20
21
22
15
16
17
18
19
20
21
22
23
16
17
18
19
20
21
22
23
24
17
18
19
20
21
22
23
24
25
18
19
20
21
22
23
24
25
26
19
20
21
22
23
24
25
26
27
20
21
22
23
24
25
26
27
28
21
22
23
24
25
26
27
28
29
After Sort:
A
B
C
D
E
F
G
H
I
J
K
L
M
N
COL10
COL9
COL8
COL7
COL6
COL5
COL4
COL3
COL2
5/11/2022
5/12/2022
5/13/2022
5/14/2022
5/15/2022
5/16/2022
5/17/2022
5/18/2022
5/19/2022
11
10
9
8
7
6
5
4
3
12
11
10
9
8
7
6
5
4
13
12
11
10
9
8
7
6
5
14
13
12
11
10
9
8
7
6
15
14
13
12
11
10
9
8
7
16
15
14
13
12
11
10
9
8
17
16
15
14
13
12
11
10
9
18
17
16
15
14
13
12
11
10
19
18
17
16
15
14
13
12
11
20
19
18
17
16
15
14
13
12
21
20
19
18
17
16
15
14
13
22
21
20
19
18
17
16
15
14
23
22
21
20
19
18
17
16
15
24
23
22
21
20
19
18
17
16
25
24
23
22
21
20
19
18
17
26
25
24
23
22
21
20
19
18
27
26
25
24
23
22
21
20
19
28
27
26
25
24
23
22
21
20
29
28
27
26
25
24
23
22
21
I'm not sure if this is a viable solution, but you could just add another sheet or use a range in columns to the right, I think you could accomplish what you want.
=transpose(sort(TRANSPOSE(filter(F:N,F:F<>"")),6,true))
See example here of before and after.
Updated to account for potential breaks in data:
=transpose(sort(TRANSPOSE(filter(Before!E:N,Row(Before!E:E)<=max(arrayformula(--NOt(ISBLANK(Before!E:N))*row(Before!E:N))))),6,true))
One could then leverage this formula in an appscript to overwrite the data with something like this:
function makeRange(){
const ss = SpreadsheetApp.getActiveSpreadsheet();
const yourSheetName = "Before"; //sheet name with data
const zFormula = "=transpose(sort(TRANSPOSE(filter('zzzzz'!E:N,Row('zzzzz'!E:E)<=max(arrayformula(--NOt(ISBLANK('zzzzz'!E:N))*row('zzzzz'!E:N))))),6,true))";//
var newSheet = ss.insertSheet();
var newFormula = zFormula.replace(/zzzzz/g,yourSheetName)
newSheet.getRange("E1").setFormula(newFormula);
var newValues = newSheet.getRange("E:N").getValues();
ss.getSheetByName(yourSheetName).getRange("E:N").setValues(newValues);
ss.deleteSheet(newSheet);
}

Integer Worksheet in Google Sheets with App Script randomizing numbers

I want to create a worksheet for adding/subtracting integers. When I run the script I would like the integers to randomly repopulate in two different columns along with the operation(addition/subtraction) in a middle column. Help please :)
24 Lines of Problems
function operatorsandintegers() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
const ops = ["-","+","*","/"];
sh.clearContents();
let vs = [...Array.from(new Array(24).keys(),x => [Math.floor(Math.random() * 100),ops[Math.floor(Math.random() * 4)],Math.floor(Math.random() * 100),''])];
vs.unshift(["Operand1","Operation","Operand2","result"]);
sh.getRange(1,1,vs.length, vs[0].length).setValues(vs);
}
Operand1
Operation
Operand2
result
1
-
74
17
/
66
14
+
89
76
+
2
72
/
27
42
+
85
34
*
62
50
+
80
33
-
40
61
*
34
30
/
66
98
/
69
27
-
26
74
-
15
44
*
14
66
-
42
43
/
64
96
-
83
52
-
29
17
+
77
30
+
34
31
/
31
26
+
67
53
+
68
Correct answers
function correctanswers() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0");
var vs = sh.getRange(2,1,sh.getLastRow() - 1,sh.getLastColumn()).getValues();
const ops = ["-","+","*","/"];
let r = vs.map(r => {
let idx = ops.indexOf(r[1]);
if(~idx) {
switch(idx) {
case 0:
return [r[0] - r[2]];
break;
case 1:
return [r[0] + r[2]];
break;
case 2:
return [r[0] * r[2]];
break;
case 3:
return [r[0]/r[2]];
break;
}
}
})
sh.getRange(2,4,r.length).setValues(r);
}
Operand1
Operation
Operand2
result
1
-
74
-73
17
/
66
0.2575757576
14
+
89
103
76
+
2
78
72
/
27
2.666666667
42
+
85
127
34
*
62
2108
50
+
80
130
33
-
40
-7
61
*
34
2074
30
/
66
0.4545454545
98
/
69
1.420289855
27
-
26
1
74
-
15
59
44
*
14
616
66
-
42
24
43
/
64
0.671875
96
-
83
13
52
-
29
23
17
+
77
94
30
+
34
64
31
/
31
1
26
+
67
93
53
+
68
121

Elasticsearch return raw JSON with Golang Beats

I'm working on a beat that interrogate our elastic search, the goal is to get elements in elastics to modify them to avoid duplication.
I've written a search and it works:
client,err :=elasticsearch.NewClient(elasticsearch.ClientSettings{URL:host,Index:indexSel,Username: username,Password: password,Timeout: 60 * time.Second,}, nil)
if err != nil {
log.Fatal(err)
}
params := map[string]string{
"q": "_id:"+maref,
}
_, resp, err := client.SearchURI(index, "", params)
if err != nil {
log.Fatal(err)
return
} else {
fmt.Println(string(resp))
}
And here is the function from the elastic api.go:
func (es *Connection) SearchURI(index string, docType string, params map[string]string) (int, *SearchResults, error) {
status, resp, err := es.apiCall("GET", index, docType, "_search", "", params, nil)
if err != nil {
return status, nil, err
}
result, err := readSearchResult(resp)
return status, result, err
}
func readSearchResult(obj []byte) (*SearchResults, error) {
var result SearchResults
if obj == nil {
return nil, nil
}
err := json.Unmarshal(obj, &result)
if err != nil {
return nil, err
}
return &result, err
}
With the following types:
type SearchResults struct {
Took int `json:"took"`
Shards json.RawMessage `json:"_shards"`
Hits Hits `json:"hits"`
Aggs map[string]json.RawMessage `json:"aggregations"`
}
type Hits struct {
Total int
Hits []json.RawMessage `json:"hits"`
}
For now the answer is a raw JSON:
&{11 [123 34 116 111 116 97 108 34 58 53 44 34 115 117 99 99 101 115 115 102 117 108 34 58 53 44 34 102 97 105 108 101 100 34 58 48 125] {1 [[123 34 95 105 110 100 101 120 34 58 34 99 111 112 105 108 98 101 97 116 45 50 48 49 54 46 49 48 46 50 53 34 44 34 95 116 121 112 101 34 58 34 73 110 99 105 100 101 110 116 34 44 34 95 105 100 34 58 34 65 86 102 54 55 69 103 109 74 66 45 119 85 116 103 82 99 90 113 97 34 44 34 95 115 99 111 114 101 34 58 49 46 48 44 34 95 115 111 117 114 99 101 34 58 123 34 64 116 105 109 101 115 116 97 109 112 34 58 34 50 48 49 54 45 49 48 45 50 53 84 48 56 58 49 54 58 53 55 46 53 53 57 90 34 44 34 97 103 101 110 116 95 105 100 34 58 49 53 44 34 98 101 97 116 34 58 123 34 104 111 115 116 110 97 109 101 34 58 34 53 55 99 53 99 49 57 49 101 48 100 57 34 44 34 110 97 109 101 34 58 34 53 55 99 53 99 49 57 49 101 48 100 57 34 125 44 34 100 101 115 99 114 105 112 116 105 111 110 34 58 34 101 108 97 115 116 105 99 34 44 34 102 105 110 97 108 99 108 97 115 115 34 58 34 34 44 34 105 100 34 58 34 73 45 48 48 53 56 50 54 34 44 34 111 114 103 95 105 100 34 58 49 55 44 34 111 114 103 95 110 97 109 101 34 58 34 83 104 97 109 34 44 34 112 114 105 111 114 105 116 121 34 58 52 44 34 114 101 102 101 114 101 110 99 101 34 58 34 73 45 48 48 53 56 50 54 34 44 34 115 116 97 114 116 95 100 97 116 101 34 58 34 50 48 49 54 45 49 48 45 50 53 84 49 48 58 49 54 58 53 54 46 56 56 49 55 55 52 49 43 48 50 58 48 48 34 44 34 115 116 97 116 117 115 34 58 34 97 115 115 105 103 110 101 100 34 44 34 116 116 111 34 58 48 44 34 116 116 111 95 49 48 48 95 116 114 105 103 103 101 114 101 100 34 58 48 44 34 116 116 114 34 58 48 44 34 116 116 114 95 49 48 48 95 116 114 105 103 103 101 114 101 100 34 58 48 44 34 116 121 112 101 34 58 34 73 110 99 105 100 101 110 116 34 125 125]]} map[]}
So my question is:
How can I convert the result to ascii?
I've read it's probably due to the json.RawMessage type that doesn't convert JSON but I can't find how to convert it.
Thanks by advance,
Plosxh.
EDIT : Solution:
Thx to icza, here's my solution I change my "fmt.Println(resp)" into:
for _, v := range resp.Hits.Hits {
fmt.Println(string(v))
}
It works like a charm!
Edit 2 :
I had to re-create a whole type en json.Unmarshal "resp.Hits.Hits" in order to use it like a regular json.
json.RawMessage is a byte slice:
type RawMessage []byte
You can convert a byte slice to a string with a simple type conversion:
var result SearchResults
// ...
for _, v := range result.Hits {
fmt.Println("Total:", v.Total, "Hits:", string(v.Hits))
}
Or:
fmt.Printf("Total: %d, Hits: %s\n", v.Total, v.Hits)
See this example:
var raw json.RawMessage
raw = json.RawMessage(`something raw`)
fmt.Println(raw)
fmt.Println(string(raw))
fmt.Printf("%s\n", raw)
Output (try it on the Go Playground):
[115 111 109 101 116 104 105 110 103 32 114 97 119]
something raw
something raw

Join query returns duplicate rows

purchase_request_master
prm_voucher_no| project_id| status_id| request_date
17 46 3 11-6-2016 0:00
18 46 3 20-6-2016 0:00
19 46 3 216-2016 0:00
purchase_request_details
prm_voucher_no| item_id| request_quantity
17 80 50
17 81 100
18 80 75
19 83 10
19 81 35
19 82 120
purchase_order_master
pom_voucher_no| prm_request_id |supplier_id
16 17 14
17 18 14
18 19 15
purchase_order_details
pom_voucher_no| approved_quantity| rate
16 50 1000
16 100 1500
17 75 150
18 10 2500
18 35 3000
18 120 1700
when I run the below query it gives 14 rows(duplicate row returning).expected out put row is 6.. Please refer below output tables..
select prm.prm_voucher_no,prm.project_id,prm.status_id,prd.requested_quantity,prd.item_id,pom.pom_voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity
from purchase_request_master prm
left join purchase_request_details prd on prd.prm_voucher_no=prm.prm_voucher_no
left join purchase_order_master pom on prm.prm_voucher_no=pom.request_id
left join purchase_order_details pod on pom.pom_voucher_no=pod.pom_voucher_no
where prm.project_id=46 and ( EXTRACT(MONTH FROM prm.request_Date)=6) and (EXTRACT(YEAR FROM prm.request_Date)=2016)
group by prm.voucher_no,prm.project_id,prm.status_id,prd.requested_quantity,prd.item_id,pom.voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity
order by prm.voucher_no
i tried inner join,distinct,distinct least,group by,temporary table,with clause all these method.. but no use every this gives duplicate row
How to solve this problem..
OUTPUT
prm_voucher_no| project_id| status_id|item_id|request_quantity |pom_voucher_no| supplier_id|approved_quantity | rate
17 46 3 80 50 16 14 100 1000
17 46 3 81 100 16 14 75 1500
17 46 3 80 75 16 15 10 150
17 46 3 81 10 16 14 35 10
18 46 3 81 35 17 14 120 35
19 46 3 80 120 18 15 50 120
19 46 3 81 50 18 14 100 1000
19 46 3 82 100 18 14 75 1500
19 46 3 80 75 18 15 10 150
19 46 3 81 10 18 14 35 10
19 46 3 82 35 18 14 120 35
19 46 3 80 120 18 15 35 120
19 46 3 81 35 18 14 50 1500
19 46 3 82 50 18 15 100 1700
EXPECTED OUTPUT
prm_voucher_no| project_id| status_id| item_id| request_quantity| pom_voucher_no| supplier_id|approved_quantity| rate
17 46 3 80 50 16 14 100 1000
17 46 3 81 100 16 14 75 1500
18 46 3 81 35 17 14 120 35
19 46 3 80 120 18 15 50 120
19 46 3 81 50 18 14 100 1000
19 46 3 82 100 18 14 75 1500
I think the problem is in your data model itself. Ideally, you would have a line_number field in both of your "detail" tables, and this would be used in the join:
create table purchase_request_details (
prm_voucher_no integer,
prm_voucher_line integer, // Add this
item_id integer,
request_quantity
)
create table purchase_order_details (
pom_voucher_no integer,
pom_voucher_line integer, // and this
approved_quantity integer,
rate integer
)
And then this query would give you the results you seek:
select
prm.prm_voucher_no,prm.project_id,prm.status_id,prd.request_quantity,
prd.item_id,pom.pom_voucher_no,pom.supplier_id,pod.rate,pod.approved_quantity
from
purchase_request_master prm
left join purchase_request_details prd on
prd.prm_voucher_no=prm.prm_voucher_no
left join purchase_order_master pom on
prm.prm_voucher_no=pom.prm_request_id
left join purchase_order_details pod on
pom.pom_voucher_no=pod.pom_voucher_no and
prd.prm_voucher_line = pod.pom_voucher_line // This is the key
where
prm.project_id=46 and
EXTRACT(MONTH FROM prm.request_Date) = 6 and
EXTRACT(YEAR FROM prm.request_Date) = 2016
order by prm.prm_voucher_no
If you have no ability to control the data model, then I think the best you can do is artificially add a line number. I don't recommend this at all, as you are presupposing a lot of things, most notably that the order of records in the one table automatically correlates to the order of records in the other -- and I'm betting that's far from a guarantee.
Adding a line number would be done using the row_number() analytic, and PostgreSQL has that but MySQL does not... you have both tags in your question. Which DBMS are you using?
If you can't add line numbers, can you add item_id to your purchase_order_details table? This would likely handle your issue, unless you can have the same item on multiple lines within a purchase request/order.
In the data you have above, a join on the requested quantity (prd.request_quantity = pod.approved_quantity) fixes your issue, but I am highly confident that this would burn you when you started running it against real data.

Got error "invalid character 'ï' looking for beginning of value” from json.Unmarshal

I use a Golang HTTP request to get json output as follow.
The web service I am trying to access is Micrsoft Translator https://msdn.microsoft.com/en-us/library/dn876735.aspx
//Data struct of TransformTextResponse
type TransformTextResponse struct {
ErrorCondition int `json:"ec"` // A positive number representing an error condition
ErrorDescriptive string `json:"em"` // A descriptive error message
Sentence string `json:"sentence"` // transformed text
}
//some code ....
body, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
return "", tracerr.Wrap(err)
}
transTransform = TransformTextResponse{}
err = json.Unmarshal(body, &transTransform)
if err != nil {
return "", tracerr.Wrap(err)
}
I got an error from invalid character 'ï' looking for beginning of value
So, I try to print the body as string fmt.Println(string(body)), it show:
{"ec":0,"em":"OK","sentence":"This is too strange i just want to go home soon"}
It seems the data doesn't have any problem, so I tried to create the same value by jason.Marshal
transTransform := TransformTextResponse{}
transTransform.ErrorCondition = 0
transTransform.ErrorDescriptive = "OK"
transTransform.Sentence = "This is too strange i just want to go home soon"
jbody, _ := json.Marshal(transTransform)
I found the original data might have problem, so I try to compare two data in []byte format.
Data from response.Body:
[239 187 191 123 34 101 99 34 58 48 44 34 101 109 34 58 34 79 75 34 44 34 115 101 110 116 101 110 99 101 34 58 34 84 104 105 115 32 105 115 32 116 111 111 32 115 116 114 97 110 103 101 32 105 32 106 117 115 116 32 119 97 110 116 32 116 111 32 103 111 32 104 111 109 101 32 115 111 111 110 34 125]
Data from json.Marshal
[123 34 101 99 34 58 48 44 34 101 109 34 58 34 79 75 34 44 34 115 101 110 116 101 110 99 101 34 58 34 84 104 105 115 32 105 115 32 116 111 111 32 115 116 114 97 110 103 101 32 105 32 106 117 115 116 32 119 97 110 116 32 116 111 32 103 111 32 104 111 109 101 32 115 111 111 110 34 125]
Any idea how I parse this response.Body and Unmarshal it into data structure?
The server is sending you a UTF-8 text string with a Byte Order Mark (BOM). The BOM identifies that the text is UTF-8 encoded, but it should be removed before decoding.
This can be done with the following line (using package "bytes"):
body = bytes.TrimPrefix(body, []byte("\xef\xbb\xbf")) // Or []byte{239, 187, 191}
PS. The error referring to ï is because the UTF-8 BOM interpreted as an ISO-8859-1 string will produce the characters .