apps script: increase variable with each execution - variables

Hi i have a list of values each value representing the output of a shift of packaging. I want to calculate the average output of 8 weeks. So each time a shift passes the average output changes. My idea is to trigger a function after each shift, which calculates the output. Now theres my problem, how do i get a varible (the one representing the row of the first value) to increase after each trigger of the function? What i tried is to declare the variable before the function and increase the variable of 1 inside the function. Buf ofc the starting value doesnt change this way.. Probably there is an easy way for this i just dont know yet (programming newbie here :)).
let i = 7;
let j = 126;
function schnitt() {
var summe = 0;
var counter = 0;
i++;
j++;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
}

I have found a work around. I just put var i and j into cells and do it like this:
function schnitt() {
var i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
var j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
var summe = 0;
var counter = 0;
while(i <= j){
var aktuell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(i,6,1,1).getValue();
if(aktuell != ""){
summe = summe + aktuell;
counter++;
i++;
}
else{
i++
}
}
var durchschnitt = summe / counter;
var ausgabe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(8,7,1,1).setValue(durchschnitt);
i = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).getValue();
i++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(3,14,1,1).setValue(i);
j = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).getValue();
j++;
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Auswertung").getRange(4,14,1,1).setValue(j);
}

Related

Cannot use bigquery udf (bqutil) in processing location: us-west-2

We are trying to use these in us-west2 - https://github.com/GoogleCloudPlatform/bigquery-utils/tree/master/udfs/community.
this first query processes just fine, in US
this second query wont run
Our dataset models is in us West 2. It seems all queries from the 2nd query editor are then processed in us-west 2 where, it seems bqutil does not exist? How can we find the function bqutil.fn.levenshtein when processing in us-west2 (where our datasets all exist)?
To use the levenshtein UDF in your BigQuery table, you need to create a UDF in the location where your dataset resides.
You can refer to the below UDF and the screenshot where the data resides in us-west2 location.
UDF :
CREATE OR REPLACE FUNCTION
`stackdemo.fn_LevenshteinDistance`(in_a STRING, in_b STRING) RETURNS INT64 LANGUAGE js AS R"""
var a = in_a.toLowerCase();
var b = in_b.toLowerCase();
if(a.length == 0) return b.length;
if(b.length == 0) return a.length;
var matrix = [];
// increment along the first column of each row
var i;
for(i = 0; i <= b.length; i++){
matrix[i] = [i];
}
// increment each column in the first row
var j;
for(j = 0; j <= a.length; j++){
matrix[0][j] = j;
}
// Fill in the rest of the matrix
for(i = 1; i <= b.length; i++){
for(j = 1; j <= a.length; j++){
if(b.charAt(i-1) == a.charAt(j-1)){
matrix[i][j] = matrix[i-1][j-1];
} else {
matrix[i][j] =
Math.min(matrix[i-1][j-1] + 1, // substitution
Math.min(matrix[i][j-1] + 1, // insertion
matrix[i-1][j] + 1)); // deletion
}
}
}
return matrix[b.length][a.length];
""";
Query :
SELECT
source,
target,
`stackdemo.fn_LevenshteinDistance`(source, target) distance,
FROM UNNEST([
STRUCT('analyze' AS source, 'analyse' AS target),
STRUCT('opossum', 'possum'),
STRUCT('potatoe', 'potatoe'),
STRUCT('while', 'whilst'),
STRUCT('aluminum', 'alumininium'),
STRUCT('Connecticut', 'CT')
]);
Output :

Adobe InDesign script to affect only selected Table/Text Frame

I am new to scripting and copied this one below and it works great but not all tables are the same in the document and I just want to affect the selected tables/text frames.
Is there an easy way to make this code work the way I am looking to do.
var myDoc = app.activeDocument;
var myWidths = [.5,.35,.44,.44];
for(var T=0; T < myDoc.textFrames.length; T++){
for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
for(var j=0; j < myWidths.length; j++){
myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
}
}
}
Thanks for any help, just starting to dive into InDesign Scripting and understand it.
Yes, it can be done quite easy:
var myWidths = [.5,.35,.44,.44];
var sel = app.selection;
if (sel.length != 1) exit();
var frame = sel[0];
if (frame.constructor.name != 'TextFrame') exit();
for (var i = 0; i < frame.tables.length; i++) {
for (var j = 0; j < myWidths.length; j++) {
frame.tables[i].columns[j].width = myWidths[j];
}
}
It will work for one selected text frame.
If you need to process several selected frames here is another variant of the code:
var myWidths = [.5,.35,.44,.44];
var frames = app.selection
var f = frames.length
while(f--) {
if (frames[f].constructor.name != 'TextFrame') continue;
var tables = frames[f].tables;
var t = tables.length;
while(t--) {
var table = tables[t];
var c = table.columns.length;
while(c--) {
table.columns[c].width = myWidths[c];
}
}
}

Needing help on printing mode in java

I am given a task to make a method that takes a parameter of an ArrayList of Integer obj and print out the sum, average, and mode.
I can't seem to figure out how to find the mode. It should print out the number if there is only one mode, and it should print out "no single mode" if there is more than one (or none) mode. My method only prints out "no single mode". How can I fix my code to have the mode printed out?
This is what I have for my code:
public static void printStatistics(ArrayList<Integer> arr){
int sum = 0;
for(int i : arr){
sum += i;
}
System.out.println("Sum: "+sum);
System.out.println("Average: "+(double)sum/arr.size());
int temp = 0, counter = 0, max = 0;
for(int j = 0; j < arr.size() - 1; j++){
for(int k = j+1; k < arr.size(); k++){
if(arr.get(j) == arr.get(k)){
counter++;
if(counter > max){
max = counter;
temp = arr.get(j);
}
if(counter == max){
temp = -1;
}
}
}
}
if(temp > 0){
System.out.println("Mode: "+temp);
}
else if(temp < 0){
System.out.println("Mode: no single mode");
}
}
The problem lies here
if(counter > max){
max = counter;
temp = arr.get(j);
}
if(counter == max){
temp = -1;
}
You are assigning the value of counter to max in the first condition so the second if condition i.e., if(counter == max) will always be true, which results in temp having the value -1 which fulfills else if(temp < 0). This is why you are getting Mode: no single mode as the output every time.
Changing the condition should give you the desired output
if(counter < max){
temp = -1;
}

Dijkstras Algorithmn Vue

I am trying to implement dijkstras pathfinding algorithm in vue. I am following the psuedocode from https://medium.com/#nicholas.w.swift/easy-dijkstras-pathfinding-324a51eeb0f. This is what I have come up with so far, however I am struggling to translate the psuedocode into vue js. How would I be able to check every node and pop the visited nodes off the list?
Dijkstra() {
this.unexploredset = [];
for (let i = 0; i < 16; i++){
for (let j = 0; j < 16; j++){
this.nodes[i][j].position = '∞';
this.nodes[i][j].distance = '∞';
if(this.nodes[i][j].hasWall == false){
this.unexploredset.push(this.nodes[i][j])
}
}
}
let current = this.nodes[3][4];
let goal = this.nodes[14][14];
for(let i = 0; i < 255; i++) {
for (let k = 0; k < 4; k++) {
if (current.distance <= current.neighbours[k].distance && current.unvisited == true)
{
current.unvisited = false;
let temp = current.neighbours[k];
current = temp
this.unexploredset.pop(current);
current = temp
if (current == goal)
{
console.log("found");
break
}
console.log(this.unexploredset.length)
}
}
}
}

AS2 optimized code

I am trying to implement a logging mechanism in my code. I have read in some forums that in AS2, string concatenation does not happen at compile time, instead it happens at runtime.
I wanted to know which of the following codes is more optimal:
for (var i:Number = 0; i < n; i++ )
{
var strToReplace:String = "{" + i + "}";
}
or,
for (var i:Number = 0; i < n; i++ )
{
var strToReplace:String = "{%s}".split("%s").join(String(i));
}
Your help would be greatly appreciated as I am a newbie as far as AS2 is concerned. Thank you.
Here is an easy way to test :
var n:Number = 100000;
var beforeTime:Number = getTimer();
for (var i:Number = 0; i < n; i++ ) {
var strToReplace:String = "{" + i + "}";
}
var betweenTime:Number = getTimer();
trace("between : " + (betweenTime - beforeTime));
for (var i:Number = 0; i < n; i++ ) {
var strToReplace:String = "{%s}".split("%s").join(String(i));
}
var afterTime:Number = getTimer();
trace("after : " + (afterTime - betweenTime));
What I get in the traces :
between : 269
after : 866
The first way is 3 times faster : split and join methods have to search inside your string to execute.
If your string is bigger, the difference is even bigger : the first methode duration doesn't change... The second one takes even more time.
You should try.