How can I use a For loop to loop tabs on sheet index numbers in Google Apps Script? - for-loop

How can I use 'i' to loop through the sheet index numbers?
I tried this:
function myFunction() {
for (var i = 2; i < 14; i++);{
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getSheets()[i].getRange('J10').setFormula('=iferror(VLOOKUP($B9,\'STUDENT SCORES\'!AL46:AM49,2,FALSE)," ")');
}
}
But it does not work.
Thanks for the help!

Use this sample script:-
function myFunction() {
var spreadsheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i = 2; i < spreadsheets.length; i++);{
spreadsheets[i].getRange('J10')
.setFormula('=iferror(VLOOKUP($B9,\'STUDENT SCORES\'!AL46:AM49,2,FALSE)," ")');
}
}

Related

Speed Up Find-and-Replace Google Apps Script Function for sheets

I've written a pretty simple script that successfully takes information from one sheet in a Google Spreadsheet, and replaces information in a column in another sheet in the same spreadsheet pending satisfaction of two criteria: the receiving row has the same "Customer ID" and "Product Type." I say "simple" because it's intuitive, but extremely computationally demanding (taking nearly 30 seconds to run!).
From what I've read online, it's the sequential read and write operations that are causing the slowdown. I'm assuming that if I sort the sheets in question on the two criteria and THEN do a function that writes over subsequent rows, I may be able to speed it up. I'm a little weak on algorithms, so I'm still scratching my head on how to do this elegantly.
Does anyone have any suggestions? Below is my original script, and I've already made sure that the spreadsheet collapses empty rows, so time isn't wasted iterating over nothing.
function replaceRawWithRepChanges(receivedSheet) {
var ss = SpreadsheetApp.openById(receivedSheet);
var repchanges = ss.getSheetByName('repchanges');
var rawSheet = ss.getSheetByName('Sheet1');
var rawTMtoReplace = rawSheet.getRange('P2:P');
var repCustID = repchanges.getRange('A1:A').getValues();
var repTM = repchanges.getRange('F1:F').getValues();
var repCategory = repchanges.getRange('G1:G').getValues();
var rawCustID = rawSheet.getRange('A2:A').getValues();
var rawTM = rawSheet.getRange('P2:P').getValues();
var rawCategory = rawSheet.getRange('U2:U').getValues();
var repInfo = [repCustID, repTM, repCategory];
var rawInfo = [rawCustID, rawTM, rawCategory];
for (var i=0; i < rawInfo[0].length; i++) {
for (var j=0; j < repInfo[0].length; j++) {
// var thisRawCust = rawInfo[0][i];
// var thisRepCust = repInfo[0][j];
if (rawInfo[0][i].toString() == repInfo[0][j].toString()) {
// var thisRawCategory = rawInfo[2][i];
// var thisRepCategory = repInfo[2][j];
if (rawInfo[2][i].toString() == repInfo[2][j].toString()) {
// var repvalue = repInfo[1][j];
rawInfo[1][i] = repInfo[1][j];
// var newRawValue = rawInfo[1][i];
}
}
}
}
return rawInfo[1];
}
Yes, you should sort the data (perhaps using the SORT command, which does work with multiple columns). Then, using two pointers, you only have to go down the columns once, rather than checking the entirety of repInfo for matches for every single row in rawInfo.
Once you've sorted the information, your loop might look like the following:
var i = 0;
var j = 0;
while (i < rawInfo[0].length && j < repInfo[0].length) {
if (rawInfo[0][i].toString() == repInfo[0][j].toString()) {
if (rawInfo[2][i].toString() == repInfo[2][j].toString()) {
rawInfo[1][i]=repInfo[1][j];
i++;
j++;
} else if (rawInfo[2][i].toString() < repInfo[2][j].toString()) {
i++;
} else {
j++;
}
} else if (rawInfo[0][i].toString() < repInfo[0][j].toString()) {
i++;
} else {
j++;
}
}

Refactoring firstReverse using each

// Trying to Refactor the firstReverse function using each? // I Created a func that takes a str as a parameter, use firstReverse within // the for loop the output will be the reversed version of the string.
var stringName = "Matt";
var firstReverse = function(str){ var output = ""; for(var i = str.length -1; i >= 0; i--){ output+= str[i]; } return output; };
firstReverse(stringName)
// Output: "ttaM"
Something like this?
var stringName="Matt";
function firstReverse(str)
{
var newString="";
str.split('').forEach(function(a){newString=a+newString});
return newString;
}
document.getElementById('test')./*danger!!!*/innerHTML = firstReverse(stringName);
<div id="test"></div>

Kendogrid population

I'm new to all this kendo stuff i need help in populating kendogrid from a csv file.
The csv data is stored in an array of strings returned by a service.
Data looks like :
0: "Module,LogLevel,LogType,LoggedTime,LogMessage"
1: "00D02D5A4B66 ,CommServer ,Level3 ,Information ,03/16/2015 00:32:57:5716 ,[ISOMMessageHandler::Initialize]-[EventCount:20,ObjectRetryCount:6]"
2: "00D02D5A4B66 ,CommServer ,Level1 ,Information ,03/16/2015 00:32:57:5716 ,ISOMProtocolHandler::HandleConnectGeneric] - Before UpdatePanelTouched - CommServerID : 1, ConnectionMode : 2"
3: "00D02D5A4B66 ,CommServer ,Level4 ,Information ,03/16/2015 00:32:57:5716 ,[PanelDataConfigurationHandler : UpdatePanelConnectionStatus] : CommServerID 1, CommMode : 2"
i need to display 0th indexed data as title of the columns
and rest in cells of the column.
My advice is to make a wrapper method yourself and get it into JSON.
needed wrapper as told by Thomas.
here is my wrapper function
function csvJSON(lines) {
var result = [];
var headers = lines[0].split(",");
headers.unshift("MAC");
for (var i = 1; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split(",");
for (var j = 0; j < headers.length; j++) {
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
return result;
}

Birt Report convert numbers to Persian numbers

I am using Birt with Eclipse Indigo.
I am using a table in the report. The table will get values from the dataset. I want to convert the numbers to Persian numbers. I tried using java script but it doesn't work.
var number = dataSetRow["invoice-number"];
var farsiNumber=new Array();
farsiNumber[0]='\u06f0';
farsiNumber[1]='\u06f1';
farsiNumber[2]='\u06f2';
farsiNumber[3]='\u06f3';
farsiNumber[4]='\u06f4';
farsiNumber[5]='\u06f5';
farsiNumber[6]='\u06f6';
farsiNumber[7]='\u06f7';
farsiNumber[8]='\u06f8';
farsiNumber[9]='\u06f9';
var outputNumber = "";
for(var i=0;i<number.length;i++) {
var ch = number.charAt(i);
var index = farsiNumber[ch];
if(ch >= 0 && ch <= 9)
outputNumber = outputNumber+index;
else
outputNumber = outputNumber+ch;
}
this.data = outputNumber;
Is there any other way of doing it or should I create a plugin for the Birt Functions plugin to do this.
Use this JavaScript function to convert numbers to Persian:
function toPersianNumber(num){
var persianNumberArray = new Array('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹')
var res = ''
while(num/10 > 0){
n = num%10
num = parseInt(num/10)
res = persianNumberArray[n] + res
}
return res
}
I'm using this function for a better performance :)
function toPersianChar(str) {
var map = ["\u0660","\u0661","\u0662","\u0663","\u0664", "\u0665","\u0666","\u0667","\u0668","\u0669"]
var replaceDigits = str.replace(/\d(?=[^<>]*(<|$))/g, function($0) { return map[$0]});
return(replaceDigits);
};

Export HTML Table to EXCEL File using php

I would like to ask for code which output a html table into excel given that the html table is in result.php
Thanks!
If your data is a pure table (i.e. no graph), you should consider using the csv (http://en.wikipedia.org/wiki/Comma-separated_values) format instead of excel. I don't knwow your skills in programmation, but this should not be hard to do.
$test="<table border=1><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header('Pragma: public');
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=myfile.xls");
echo $test;
function exporttoexcel(){
var i;
var j;
var mycell;
var tableID = "crtfdetails";
var objXL = new ActiveXObject("Excel.Application");
var objWB = objXL.Workbooks.Add();
var objWS = objWB.ActiveSheet;
for (i=0; i < document.getElementById(tableID).rows.length; i++)
{
for (j=0; j < document.getElementById(tableID).rows(i).cells.length; j++)
{
mycell = document.getElementById(tableID).rows(i).cells(j)
objWS.Cells(i+1,j+1).Value = mycell.innerText;
}
}
objWS.Range("A1", "Z1").EntireColumn.AutoFit();
objXL.Visible = true;
}

Resources