i got a little problem with my code: i got 3 pieces of code, but i can't combine it together.
All three codes put timestamps in different cells when certain cells change.
If I change cell A1, then in cell B1 insert timestamp. If the change in cell A2, then in cell B2 insert timestamp , etc.
Can you help me? Thanks.
1)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 17;
var rcol = 17;
var tcol = 18;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
2)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 15;
var rcol = 15;
var tcol = 16;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
3)
function onEdit(event) {
var tsheet = 'Заявки' ;
var lcol = 9;
var rcol = 9;
var tcol = 8;
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
if (scol >= lcol && scol <= rcol) {
s.getRange(r.getRow(), tcol).setValue(new Date());
}
}
}
Put the column specification in an array:
function onEdit(event) {
var tsheet = 'Заявки' ;
var colspec=[[17,17,18],[15,15,16],[9,9,8]];
var s = event.source.getActiveSheet();
var sname = s.getName();
if (sname == tsheet) {
var r = event.source.getActiveRange();
var scol = r.getColumn();
var i=colspec.length;
while (i>0) {
i--;
if (scol >= colspec[i][0] && scol <= colspec[i][1]) {
s.getRange(r.getRow(), colspec[i][2]).setValue(new Date());
}
}
}
}
Related
I've to make a code that run a matrix to find the sorthest path to '9' (only walking above the ones, and avoiding the zeros)
In the example below The result correct (5 steps), but if I change the matrix elements positions, the result came wrong..
What can I do? I appreciate any help.
var matrix =
[[1,1,1,1],
[0,1,1,0],
[0,1,0,1],
[0,1,9,1],
[1,1,1,1]]
function runMatrix(matrix){
newPos = 0;
var resultr;
var resultc;
for( var i = 0, lenR = matrix.length; i < lenR; i++ ) {
for(var j = 0, lenC = matrix[i].length; j < lenC; j++){
if( matrix[i][j] == 9 ) {
resultr = i;
resultc = j;
break;
}
}
}
for( var i = 0; i < resultr; i++ ) {
var k = 0;
if(matrix[i+1][k] == 1){
if(matrix[i+1][k+1] == 9 || matrix[i+1][k-1] == 9){
newPos = newPos + 2;
}else{
newPos = newPos + 1;
}
}else{
for(k = 1; k < resultc; i++){
if(matrix[i][k] == 1 || matrix[i][k] == 9){
newPos = newPos + 1
if(matrix[i+1][k] == 1){
if(matrix[i+1][k+1] == 9 || matrix[i+1][k-1] == 9){
newPos = newPos + 2;
}else{
newPos = newPos + 1;
}
i++;
break;
}
}
}
}
}
console.log("Steps: "+newPos)
}
runMatrix(matrix)
update
this matrix doens't work, for example:
[[1,1,1,1],
[0,0,0,1],
[0,0,0,1],
[0,0,9,1],
[0,0,0,0]]
It returns 4
I want to know if the first object is in the same position of the second object to alert a message.
I am trying to create a simple game to see if the falling object touches the object that I move with my keyboard to stop the game and write "game over".
var t = setInterval(move,50);
var b = document.getElementById("ball");
var u = document.getElementById('obj') ;
function move() {
var pob = parseInt(b.style.top);
if (pob + 30 >=350){
pob = 0;
var p = Math.floor(Math.random()*320);
b.style.left = p + "px";
}
pob += 10;
b.style.top=pob+"px";
var f = parseInt(b.style.top) ;
var box = parseInt(u.style.top);
if (f == box) {
alert("stop");
}
}
Something like this?
(Example from https://www.w3schools.com/graphics/game_obstacles.asp)
function touch(obj1,obj2) {
var myleft = obj1.offsetLeft;
var myright = obj1.offsetLeft+obj1.offsetWidth;
var mytop = obj1.offsetTop;
var mybottom = obj1.offsetTop+obj1.offsetHeight;
var otherleft = obj2.offsetLeft;
var otherright = obj2.offsetLeft+obj2.offsetWidth;
var othertop = obj2.offsetTop;
var otherbottom = obj2.offsetTop+obj2.offsetHeight;
return !((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright))
}
var b = document.getElementById("ball");
var u = document.getElementById('obj') ;
var s = document.getElementById('sep') ;
console.log(touch(b,u),touch(s,b))
<div id='ball'>b</div>
<div id='sep'>separator</div>
<div id='obj'>u</div>
EDIT:
And there is the full snippet:
var t = setInterval(move,50);
var b = document.getElementById("ball");
var u = document.getElementById('obj') ;
var pos = 10 ;
var posleft =100;
var divwidth = 300;
document.addEventListener('keydown', function(event)
{
if(event.keyCode == 37) {
posleft = posleft - pos ;
if (posleft<0) {posleft = 0;}
u.style.left=posleft+'px';
}
else if(event.keyCode == 39) {
posleft = posleft + pos;
if (posleft>divwidth) {posleft = divwidth}
u.style.left=posleft+'px';
}
});
function touch(obj1,obj2) {
var myleft = obj1.offsetLeft;
var myright = obj1.offsetLeft+obj1.offsetWidth;
var mytop = obj1.offsetTop;
var mybottom = obj1.offsetTop+obj1.offsetHeight;
var otherleft = obj2.offsetLeft;
var otherright = obj2.offsetLeft+obj2.offsetWidth;
var othertop = obj2.offsetTop;
var otherbottom = obj2.offsetTop+obj2.offsetHeight;
return !((mybottom < othertop) ||
(mytop > otherbottom) ||
(myright < otherleft) ||
(myleft > otherright))
}
function move() {
if (touch(b,u)) {
alert("stop");
clearInterval(t)
}else{
var pob = parseInt(b.style.top);
if (pob + 30 >=200){
pob = 0;
var p = Math.floor(Math.random()*320);
b.style.left = p + "px";
}
pob += 5;
b.style.top=pob+"px";
}
}
<div id="obj" style="position:absolute;left:100px;top:180px;border:solid green 1px;">obj</div>
<div id="ball" style="position:absolute;top:0px;border:solid red 1px;">ball</div>
You need to focus on the page (click it) to control the game!
I hope that this will help you!
I have to fadeIn and fadeOut the information of the setInterval at the end of the program: I want that after certain amount of time, the information slowly disappear and then reappear after the information slowly. I saw other example of this subject but I didn't find a solution, i don't know how to do it, any suggestion? (Excuse me for my horrible english).
var newsTitle = [];
var newsDate = [];
var pubDate = [];
var newsHours = [];
var newsLenght;
var resultDate = [];
var cDate = 0;
var w = 1;
var newsName = "La Repubblica";
function newsUpdateAll() {
cDate = new Date();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
extractData(xhttp);
}
};
xhttp.open("GET", "http://www.repubblica.it/rss/homepage/rss2.0.xml", true);
xhttp.send();
}
function extractData(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("item");
for (i = 0; i < x.length; i++) {
newsTitle[i] = x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
pubDate[i] = x[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
var newnewsDate = new Date(pubDate[i]);
newsDate[i] = ((cDate - newnewsDate)/1000)/60;
}
newsLenght = x.lenght;
newsClassification();
setRules();
document.getElementById("news-title").innerHTML = newsTitle[0];
document.getElementById("news-name").innerHTML = newsName + ", " + newsHours[0];
}
function newsClassification() {
var length = newsDate.length;
for (var i = (length - 1); i >= 0; i--) {
for (var j = (length - i); j > 0; j--) {
if(newsDate[j] < newsDate[j-1]) {
var tmp = newsDate[j];
newsDate[j] = newsDate[j-1];
newsDate[j-1] = tmp;
var tmp2 = newsTitle[j];
newsTitle[j] = newsTitle[j-1];
newsTitle[j-1] = tmp2;
}
}
}
}
function setRules(){
for (i = 0; i < newsDate.length; i++) {
resultDate[i] = Math.round(newsDate[i]);
if (resultDate[i] >= 1440){
newsHours[i] = Math.round((resultDate[i] / 60) / 24);
if (newsHours[i] > 1){
newsHours[i] = newsHours[i] + " giorni fa:";
}else{
newsHours[i] = newsHours[i] + " giorno fa:";
}
}else if (resultDate[i] >= 60){
newsHours[i] = Math.round(resultDate[i] / 60);
if (newsHours[i] > 1){
newsHours[i] = newsHours[i] + " ore fa:";
}else{
newsHours[i] = newsHours[i] + " ora fa:";
}
}else{
newsHours[i] = resultDate[i];
if (newsHours[i] > 1){
newsHours[i] = newsHours[i] + " minuti fa:";
}else{
newsHours[i] = newsHours[i] + " minuto fa:";
}
}
}
}
newsUpdateAll();
setInterval(function (){
if (w > 55){
w = 1;
newsUpdateAll();
}
document.getElementById("news-title").innerHTML = newsTitle[w];
document.getElementById("news-name").innerHTML = newsName + ", " + newsHours[w];
w++;
},20000);
I got text from internet with node.js and want to convert it from gbk encoding to utf-8.
I tried to the node-iconv module, it didn't work.
var Iconv = require('iconv').Iconv;
var gbk_to_utf8 = new Iconv('gbk', 'utf-8');
var b = gbk_to_utf8.convert(new Buffer(body.toString()));
console.log(b.toString());
Try this code from this link:
GB2312UTF8 = {
Dig2Dec : function(s){
var retV = 0;
if(s.length == 4){
for(var i = 0; i < 4; i ++){
retV += eval(s.charAt(i)) * Math.pow(2, 3 - i);
}
return retV;
}
return -1;
} ,
Hex2Utf8 : function(s){
var retS = "";
var tempS = "";
var ss = "";
if(s.length == 16){
tempS = "1110" + s.substring(0, 4);
tempS += "10" + s.substring(4, 10);
tempS += "10" + s.substring(10,16);
var sss = "0123456789ABCDEF";
for(var i = 0; i < 3; i ++){
retS += "%";
ss = tempS.substring(i * 8, (eval(i)+1)*8);
retS += sss.charAt(this.Dig2Dec(ss.substring(0,4)));
retS += sss.charAt(this.Dig2Dec(ss.substring(4,8)));
}
return retS;
}
return "";
} ,
Dec2Dig : function(n1){
var s = "";
var n2 = 0;
for(var i = 0; i < 4; i++){
n2 = Math.pow(2,3 - i);
if(n1 >= n2){
s += '1';
n1 = n1 - n2;
}
else
s += '0';
}
return s;
},
Str2Hex : function(s){
var c = "";
var n;
var ss = "0123456789ABCDEF";
var digS = "";
for(var i = 0; i < s.length; i ++){
c = s.charAt(i);
n = ss.indexOf(c);
digS += this.Dec2Dig(eval(n));
}
return digS;
},
GB2312ToUTF8 : function(s1){
var s = escape(s1);
var sa = s.split("%");
var retV ="";
if(sa[0] != ""){
retV = sa[0];
}
for(var i = 1; i < sa.length; i ++){
if(sa[i].substring(0,1) == "u"){
//alert(this.Str2Hex(sa[i].substring(1,5)));
retV += this.Hex2Utf8(this.Str2Hex(sa[i].substring(1,5)));
if(sa[i].length){
retV += sa[i].substring(5);
}
}
else{
retV += unescape("%" + sa[i]);
if(sa[i].length){
retV += sa[i].substring(5);
}
}
}
return retV;
},
UTF8ToGB2312 : function(str1){
var substr = "";
var a = "";
var b = "";
var c = "";
var i = -1;
i = str1.indexOf("%");
if(i==-1){
return str1;
}
while(i!= -1){
if(i<3){
substr = substr + str1.substr(0,i-1);
str1 = str1.substr(i+1,str1.length-i);
a = str1.substr(0,2);
str1 = str1.substr(2,str1.length - 2);
if(parseInt("0x" + a) & 0x80 == 0){
substr = substr + String.fromCharCode(parseInt("0x" + a));
}
else if(parseInt("0x" + a) & 0xE0 == 0xC0){ //two byte
b = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
var widechar = (parseInt("0x" + a) & 0x1F) << 6;
widechar = widechar | (parseInt("0x" + b) & 0x3F);
substr = substr + String.fromCharCode(widechar);
}
else{
b = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
c = str1.substr(1,2);
str1 = str1.substr(3,str1.length - 3);
var widechar = (parseInt("0x" + a) & 0x0F) << 12;
widechar = widechar | ((parseInt("0x" + b) & 0x3F) << 6);
widechar = widechar | (parseInt("0x" + c) & 0x3F);
substr = substr + String.fromCharCode(widechar);
}
}
else {
substr = substr + str1.substring(0,i);
str1= str1.substring(i);
}
i = str1.indexOf("%");
}
return substr+str1;
}
};
And to test the function:
GBK => UTF8:
var utf8 = GB2312UTF8.GB2312ToUTF8("中文GB2312");
UTF8 => GBK:
GB2312UTF8.UTF8ToGB2312(utf8);
I was told I should consolidate my if statements. I'm not sure how to do this? Also, is there anything else wrong in this script? It is for a google doc script.
function onEdit(e) {
var colorA = "yellow";
var colorB = "#dddddd";
var colorC = "#dddddd";
var sheet = e.source.getActiveSheet();
var range = e.source.getActiveRange();
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
sheet.insertRowAfter(range.getRow());
var r = range.getRow() + 1;
sheet.getRange("A" + r + ":H" + r).setBackgroundColor(colorC);
}
}
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
var rows = sheet.getMaxRows();
//two ranges
//column C
var rangeC = sheet.getRange("C1:C"+rows);
var valuesC = rangeC.getValues();
//column H range
var rangeH = sheet.getRange("H1:H"+rows);
var colorH = rangeH.getBackgroundColors();
var valuesH = rangeH.getValues();
//iterate over each row in column C and H
//then change color
for (var row = 0; row < valuesC.length; row++) {
//check for columnC and column H
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
}
sheet.getRange("H1:H" + rows).setBackgroundColors(colorH);
}
}
Here is the other one
ss = SpreadsheetApp.getActiveSpreadsheet();
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "New PO", functionName: "NewPO"}];
ss.addMenu("New PO", menuEntries);
}
function NewPO() {
SpreadsheetApp.getActiveSheet().insertRowsBefore(1,6);
// Adjust this range accordingly, these are the cells that will be
// copied. Format is getRange(startRow, startCol, numRows, numCols)
ss.getSheetByName("PO Form").getRange(1, 1, 6, 8)
.copyTo(SpreadsheetApp.getActiveSheet().getRange(1, 1, 6, 8));
}
function onEdit(e) {
var ss = e.source.getActiveSheet();
var r = e.source.getActiveRange();
// 1 is A, 2 is B, ... 8 is H
if (r.getColumn() == 8 && r.getValue() == "x") {
r.setValue(Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd"));
}
}
Besides what murray noted, there are several instances where you repeat the same expression:
if (e.source.getActiveRange().getColumn() == 3 ||
e.source.getActiveRange().getColumn() == 8) {
could be:
var col = e.source.getActiveRange().getColumn();
if(col == 3 || col == 8) {
This applies to a lesser extent to:
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
colorH[row][0] = colorA;
} else if (valuesH[row][0] != "") {
colorH[row][0] = colorB;
}
which could be (for instance):
var hRow = colorH[row];
if (valuesC[row][0] != "" && valuesH[row][0] == "") {
hRow[0] = colorA;
} else if (valuesH[row][0] != "") {
hRow[0] = colorB;
}
only thing i can see:
// 3 is column C
if (range.getColumn() == 3) {
if (range.getValue() != "") {
// 3 is column C
if (range.getColumn() == 3 && range.getValue() != "") {