var months = ["jan", "feb"];
I am using processing.JS
var mouseClicked = function(){
if(mouseX < 78 && mouseX > 12){
months = months + 1;
}
gives me j.
How do I make it so that it goes from jan to feb and so on?
You need a separate index variable which you add 1 to. Something like this:
var index = 0;
var months = ["jan", "feb"];
var mouseClicked = function(){
if(mouseX < 78 && mouseX > 12){
index = index + 1;
}
}
var monthName = months[index];
More info is available in the Processing.js reference.
Related
I have 6 documents each with the format below
Header
Text
table 1
Bullet Points
table 2
Text
I want to combine them into one doc - however when I tried this code it dose not work.Please if anyone can advise please
`
function mergeDocs() {
var docIDs = ['list-of','documents','ids','you should have somehow'];
var baseDoc = DocumentApp.openById(docIDs[0]);
var body = baseDoc.getActiveSection();
for( var i = 1; i < docIDs.length; ++i ) {
var otherBody = DocumentApp.openById(docIDs[i]).getActiveSection();
var totalElements = otherBody.getNumChildren();
for( var j = 0; j < totalElements; ++j ) {
var element = otherBody.getChild(j).copy();
var type = element.getType();
if( type == DocumentApp.ElementType.PARAGRAPH )
body.appendParagraph(element);
else if( type == DocumentApp.ElementType.TABLE )
body.appendTable(element);
else if( type == DocumentApp.ElementType.LIST_ITEM )
body.appendListItem(element);
else
throw new Error("According to the doc this type couldn't appear in the body: "+type);
}
}
`
I am trying to script something in Photoshop that has to run every X seconds. In JavaScript it would look like this:
function run() {
alert('Ran!')
}
setInterval(run, 1000)
But in Photoshop's JavaScript, "setInterval is not a function". Any idea how else I would get any form of interval working?
This is by no means a good example, however, it stalls for a second. Those that don't know it there's no pause or sleep function in ECMA 3. I don't use a while loop as that's asking for trouble in Photoshop and may lock things up - which may lose your work.
sleepy(1000)
function sleepy(milliseconds)
{
// Start the fans please! I mean timer
var dStart = new Date().getTime();
var longtime = 10000000;
for(var i = 0 ; i < longtime; i++)
{
var now = new Date().getTime();
if (now > dStart + milliseconds)
{
// Stop the fans!
var dEnd = new Date().getTime();
var timeTaken = (dEnd - dStart)/1000;
var msgTime = (timeTaken + " seconds.");
alert(msgTime);
// alert("Ran");
return;
}
}
}
or if it's just a second, just calculate the Fibbonaci sequence to 28 spots. It's also machine dependant (and rather slower than expected in Photoshop)
function fibonacci(n)
{
if (n < 2)
return n
else
{
return fibonacci(n-1) + fibonacci(n-2);
}
}
var numOfNums = 28; // 0.91 seconds
// var numOfNums = 29; // 1.47 seconds
// var numOfNums = 30; // 2.39 seconds
// var numOfNums = 31; // 3.8 seconds
var dStart = new Date().getTime();
var msg = "";
for(var i = 0 ; i < numOfNums; i++)
{
msg += fibonacci(i) + ", ";
}
var dEnd = new Date().getTime();
var timeTaken = (dEnd - dStart)/1000;
// alert(msg);
alert(timeTaken);
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);
}
so I'm a long way form a competent programmer, but I do dabble in google sheets scripts.
I previously had a script running on a timer trigger to hide rows based on dates found in column A.
function min() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("OLD");
var v = s.getRange("A:A").getValues();
var today = new Date().getTime() - 86400000
for (var i = s.getLastRow(); i > 1; i--) {
var t = v[i - 1];
if (t != "") {
var u = new Date(t);
if (u < today) {
s.hideRows(i);
}
}
}
}
function max() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("OLD");
var v = s.getRange("A:A").getValues();
var today = new Date().getTime() + 86400000
for (var i = s.getLastRow(); i > 1; i--) {
var t = v[i - 1];
if (t != "") {
var u = new Date(t);
if (u > today) {
s.hideRows(i);
}
}
}
}
function SHOWROWS() {
// set up spreadsheet and sheet
var ss = SpreadsheetApp.getActiveSpreadsheet(), sheets = ss.getSheets();
for(var i = 0, iLen = sheets.length; i < iLen; i++) {
// get sheet
var sh = sheets[i];
// unhide rows
var rRows = sh.getRange("A:A");
sh.unhideRow(rRows);
}
}
this would be set to run at midnight every night so as to hide all rows not +-1 day from the current date.
I have now switched to using this document primarily through the android app I need to swap the input layout Moving dates from rows and values in columns to the inverse, values are now in rows and the dates are in columns, inputing vertical values would be much quicker....but honestly its more about understanding what im doing wrong thats really motivating my search for a answer.
can anyone help me modify my old script to work on this changed sheet.
my attempts fall flat..eg:
function min() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName("OLD");
var v = s.getRange("1:1").getValues();
var today = new Date().getTime() - 86400000
for (var i = s.getLastColumn(); i > 1; i--) {
var t = v[i - 1];
if (t != "") {
var u = new Date(t);
if (u < today) {
s.hidecolumns(i);
}
}
}
}
example spreadsheet:
https://docs.google.com/spreadsheets/d/1EjRIeDPrG_qp1S9QhInl9r3G0cZx_16OvnTXORgA3n4/edit?usp=sharing
there is two sheets one with the old version of my layout called "OLD" and my new desired layout called "NEW"
thanks in advance to anyone able to help
This function hides a column whose top row date is less yesterday.
function hideColumnWhenTopRowDateIsBeforeYesterday() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName("Sheet1");
var rg=sh.getRange(1,1,1,sh.getLastColumn());
var vA=rg.getValues()[0];
var yesterday=new Date(new Date().getFullYear(),new Date().getMonth(),new Date().getDate()-1).valueOf();
vA.forEach(function(e,i){
if(new Date(e).valueOf()<yesterday) {
sh.hideColumns(i+1);
}
});
}
My Spreadsheet before hiding columns:
My Spreadsheet after hiding columns:
What is the best way to copy the value of one variable to another without a direct assignment function?
Variables are 2 byte fixed point numbers with a max value of 2147483.647. The available operators are (reformatted for clarity):
add(<variable>, <constant>) which adds a constant value to the given variable
subtract(<variable>, <constant>) which subtracts
multiply(<variable>, <constant>) which multiplies
divide(<variable>, <constant>) which divides
check(<variable1>, <op>, <variable2/constant>) which compares the value of variable1 with that of <variable2/constant> using the operator op and returns a boolean. op can be =, <, <=, >= or >
set(<variable>, <constant>) which sets the value of the variable.
The scripting language has basic control flow, it has while and if (but not else) as well as basic boolean operators AND, OR, NOT and NOR.
Our first attempt was simply:
while = {
count = var1 //loop var1 times
change_variable = { which = var2 value = 1 } //increment by one
}
My next attempt was:
set_variable = { which = new value = 1 }
while = {
limit = { NOT = { check_variable = { which = old value = new} } }
if = {
limit = {
check_variable = { which = old value > new }
check_variable = { which = stage value = 0 }
}
multiply_variable = { which = new value = 10 }
}
if = {
limit = {
check_variable = { which = stage value = 0 }
check_variable = { which = old value < new }
}
set_variable = { which = stage value = 1 }
}
if = {
limit = {
check_variable = { which = stage value = 1 }
check_variable = { which = old value < new }
}
subtract_variable = { which = new value = 1 }
}
}
E.g., start with 1, multiply by 10 until greater than the target, then subtract 1 until at the target.
But I'm sure there's a much better way to do this.
Context:
A recent patch of the scripting language used in the game "Stellaris" has broken the variable assignment function. set above should be set(<variable>, <variable/constant>). This has broken a lot of existing code, and it's unknown how long it will remain broken.
because 2147483.647 is a (power of 2 - 1) /1000 I can use the successive powers of 2 to approach the solution 1 bit at a time: (written in C using only equivalent operations)
result = 0;
result += 1073741.824;
if( var < result )
result -= 1073741.824;
result += 536870.912;
if( var < result)
result -= 536870.912;
result += 268435.456;
if( var < result)
result -= 268435.456;
result += 134217.728;
if( var < result)
result -= 134217.728;
result += 67108.864;
if( var < result)
result -= 67108.864;
result += 33554.432;
if( var < result)
result -= 33554.432;
result += 16777.216;
if( var < result)
result -= 16777.216;
result += 8388.608;
if( var < result)
result -= 8388.608;
result += 4194.304;
if( var < result)
result -= 4194.304;
result += 2097.152;
if( var < result)
result -= 2097.152;
result += 1048.576;
if( var < result)
result -= 1048.576;
result += 524.288;
if( var < result)
result -= 524.288;
result += 262.144;
if( var < result)
result -= 262.144;
result += 131.072;
if( var < result)
result -= 131.072;
result += 65.536;
if( var < result)
result -= 65.536;
result += 32.768;
if( var < result)
result -= 32.768;
result += 16.384;
if( var < result)
result -= 16.384;
result += 8.192;
if( var < result)
result -= 8.192;
result += 4.096;
if( var < result)
result -= 4.096;
result += 2.048;
if( var < result)
result -= 2.048;
result += 1.024;
if( var < result)
result -= 1.024;
result += 0.512;
if( var < result)
result -= 0.512;
result += 0.256;
if( var < result)
result -= 0.256;
result += 0.128;
if( var < result)
result -= 0.128
result += 0.064;
if( var < result)
result -= 0.064;
result += 0.032;
if( var < result)
result -= 0.032;
result += 0.016;
if( var < result)
result -= 0.016;
result += 0.008;
if( var < result)
result -= 0.008;
result += 0.004;
if( var < result)
result -= 0.004;
result += 0.002;
if( var < result)
result -= 0.002;
result += 0.001;
if( var < result)
result -= 0.001;
only 32 comparisons needed (less if you can live with less precision or know the upper bound).