I'm trying to generate some random data points for my Highcharts series, but I'm having issues with the data function. Here's my code (simplified from Fiddle):
series : {
name : 'Total Mentions',
type:'spline',
lineWidth:1,
data : (function() {
var arr = [];
for(var i = 0; i < 500; i++) {
var date = randomDate(new Date(2004, 0, 9), new Date());
var randNum = Math.round(Math.random()*100);
var finalDate = "Date.UTC(" + date.getFullYear() + ", " + date.getDate() + ", " + date.getMonth() + ")";
arr.push([finalDate, randNum]);
}
return arr;
})()
},
[...etc...],
The format that should be coming out should look like this:
[Date.UTC(2008, 23, 8),56],
[Date.UTC(2012, 12, 6),21],
[Date.UTC(2008, 22, 10),16],
[Date.UTC(2009, 17, 7),25],
[...etc...],
Right now, my page isn't loading the chart. The page will load infinitely, as if it's not recognizing the data.
Any thoughts?
You should push date in millisec in array, like this;
arr.push([date.getTime(), randNum]);
arr.sort(function (a,b) { if (a[0] < b[0]) return -1; if (a[0] > b[0]) return 1;
return 0; })
return arr;
I've created a fiddle at; http://jsfiddle.net/hkskoglund/cnTqS/4/
Try catching other syntax errors in the console in Chrome devtools.
Related
Below I have some code I have running for a spreadsheet. Right now it takes a min or two to run through the script. I was wondering if anyone has any suggestions on how to re-work my code to run a little faster.
What the code does is search on a tab in the sheet called "set up" for check-marked items in a list that I would like included in my "Master Sheet". Then go to my sheet which contains all of the information that I would like copied and pasted over according to what is check marked on my set-up page. Then copy and paste those line items to the master sheet.
function allToMaster(){
var sss = SpreadsheetApp.getActive();
var ssAll = sss.getSheetByName("FF All");
var ssMaster = sss.getSheetByName("FF Master");
var ssSetup = sss.getSheetByName("FF Setup");
ssMaster.clear();
var masterCounter = 2;
ssAll.getRange("P:P").clear();
var sourceRange = ssAll.getRange(1,1,1,15);
sourceRange.copyTo(ssMaster.getRange(1,1));
//get last row of FF All
var lastRowAll = ssAll.getLastRow();
var lastRowMaster = ssMaster.getLastRow();
ssAll.getRange("P2:P" + lastRowAll).setFormula("=index('FF Setup'!B:B,match(B2,'FF Setup'!C:C,0))");
ssMaster.setRowHeightsForced(2, 500, 26);
for (i=2;i<=lastRowAll;i++){
if (ssAll.getRange(i,1).getBackground() == "#a8d08d"){
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
masterCounter++;
} else if (ssAll.getRange(i,1).getBackground() == "#e2efd9"){
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
masterCounter++;
} else {
if (ssAll.getRange("P" + i).getValue() == true) {
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
ssMaster.setRowHeightsForced(masterCounter, 1, 136);
masterCounter++;
}
}
}
ssAll.getRange("P:P").clear();
//Clear Empty Subtitles
var lastRowMaster = ssMaster.getLastRow();
for (i=2;i<=lastRowMaster;i++){
if (ssMaster.getRange(i,1).getBackground() == "#e2efd9"){
if(ssMaster.getRange((i+1),1).getBackground() == "#e2efd9" || ssMaster.getRange((i+1),1).getBackground() == "#a8d08d"){
ssMaster.deleteRow(i);
ssMaster.insertRowAfter(500);
i=i-1;
}
}
}
//Clear Empty Titles
var lastRowMaster = ssMaster.getLastRow();
for (i=2;i<=lastRowMaster;i++){
if (ssMaster.getRange(i,1).getBackground() == "#a8d08d"){
if(ssMaster.getRange((i+1),1).getBackground() == "#a8d08d"){
ssMaster.deleteRow(i);
ssMaster.insertRowAfter(500);
i=i-1;
}
}
}
//Find the row with "Delivery"
var deliveryRow = getRowOf("DELIVERY", "FF All", 1);
var sourceRange = ssAll.getRange(deliveryRow,1,(lastRowAll - deliveryRow + 1),15);
var masterCounter = ssMaster.getLastRow()
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
masterCounter = masterCounter + lastRowAll - deliveryRow - 2;
//.setFormula('=SUMA(J264:J275)');
// ssMaster.getRange(masterCounter, 10).setFormula("=sum(J2:J" + (masterCounter - 1) + ")");
//ssMaster.getRange(masterCounter, 11).setFormula("=sum(K2:K" + (masterCounter - 1) + ")");
//ssMaster.getRange(masterCounter, 13).setFormula("=sum(M2:M" + (masterCounter - 1) + ")");
//ssMaster.getRange(masterCounter, 15).setFormula("=M" + masterCounter + " - K" + masterCounter);
}
function getRowOf(value, sheet, col){
var dataArr = SpreadsheetApp.getActive().getSheetByName(sheet).getRange(4, col, 3500, 1).getValues();
for(var j = 0; j < dataArr.length; j ++){
var currVal = dataArr[j][0];
if(currVal == value){
return j+4;
break;
}
}
return 0;
}
You need to change the loops as they are doing several calls to Class SpreadsheetApp on each iteration.
Regarding the first loop,
for (i=2;i<=lastRowAll;i++){
if (ssAll.getRange(i,1).getBackground() == "#a8d08d"){
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
masterCounter++;
} else if (ssAll.getRange(i,1).getBackground() == "#e2efd9"){
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
masterCounter++;
} else {
if (ssAll.getRange("P" + i).getValue() == true) {
var sourceRange = ssAll.getRange(i,1,1,15);
sourceRange.copyTo(ssMaster.getRange(masterCounter,1));
ssMaster.setRowHeightsForced(masterCounter, 1, 136);
masterCounter++;
}
}
}
Instead of getting the background of one cell at a time (ssAll.getRange(i,1).getBackground()), before the loop get the backgrounds of all the cells before the loop, i.e.
const backgrounds = ssAll.getRange(2,1,lastRowAll).getBackgrounds();
then replace ssAll.getRange(i,1).getBackground() by backgrounds[i-1][0].
Do the something similar about ssAll.getRange("P" + i).getValue(), before the loop get the all values of the P column:
const values = ssAll.getRange("P" + i + ":P" + lastRowAll).getValues()
then replace ssAll.getRange("P" + i).getValue() by values[i-1][0]`.
It might be also possible to optimize further the first loop depending on if you really need to copy the ranges (besides values, include borders, background, notes, etc.) or if you only need the values.
Another option is to use the Advances Sheets Services but this implies to make a completely different implementation.
I'm doing RolePlay Character Sheets on a "Parent tab" I've called "MODEL", where I masterize my formulas.
I've created a second tab "Character1" and a third one "Character2". But when I try to use =QUERY or =TEXTFORMULA or whatever. It doesn't make the formulas to calculate on the actual spreadsheet, it just get the data from the "MODEL" tab.
My only way is actually to copy/past all my formulas, but if I do a mistake, I'll have to correct it in every spreadsheet every time.
Is that possible to have a formula which take the cell at:
MODELE!AE58
And automatically generate the same formulas in every tabs:
CHARACTER1!AE58
CHARACTER2!AE58
etc...
Sorry if its blur, I'm doing my best to explain.
simple
Try
function onEdit(e) {
var sh = e.source.getActiveSheet()
var rng = e.source.getActiveRange()
if (rng.getFormula() != '' && sh.getName() == 'MODEL') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(sh => {
if (!~excl.indexOf(sh.getSheetName())) {
sh.getRange(rng.getA1Notation()).setFormula(rng.getFormula())
}
})
}
}
when you change a formula in MODEL, this will also change in other tabs excepts excluded ones
multiple
If you edit the formulas by dragging them into the MODEL sheet, use this one which allows you to edit all the formulas at once
function onEdit(e) {
var sh = e.source.getActiveSheet()
if (sh.getName() != 'MODEL') return;
for (var i = e.range.rowStart; i <= e.range.rowEnd; i++) {
for (var j = e.range.columnStart; j <= e.range.columnEnd; j++) {
if (sh.getRange(i, j).getFormula() != '') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(child => {
if (!~excl.indexOf(child.getSheetName())) {
child.getRange(sh.getRange(i, j).getA1Notation()).setFormula(sh.getRange(i, j).getFormula())
}
})
}
}
}
}
global
Il you need to reset all formulas, enable google sheets api and try
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem('👉 Apply all formulas from MODEL to all tabs', 'spreadFormulas')
.addToUi();
}
function spreadFormulas() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('MODEL')
if (sh.getName() != 'MODEL') return;
var data = [];
var formulas = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn()).getFormulas()
for (var i = 0; i < formulas.length; i++) {
for (var j = 0; j < formulas[0].length; j++) {
if (formulas[i][j] != '') {
var excl = ['MODEL', 'OTHER'];//excluded sheets
SpreadsheetApp.getActiveSpreadsheet().getSheets().forEach(child => {
if (!~excl.indexOf(child.getSheetName())) {
data.push({
range: `${child.getName()}!${columnToLetter(+j + 1) + (+i + 1)}`,
values: [[`${formulas[i][j]}`]],
})
}
})
}
}
}
if (data.length) {
var resource = {
valueInputOption: 'USER_ENTERED',
data: data,
};
try { Sheets.Spreadsheets.Values.batchUpdate(resource, ss.getId()); } catch (e) { console.log(JSON.stringify(e)) }
}
}
function columnToLetter(column) {
var temp, letter = '';
while (column > 0) {
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
if your sheet is called MODELE try on some other sheet just:
=MODELE!AE58
for array it would be:
={MODELE!AE58:AE100}
also take a look into "Named Ranges" - maybe you will find it more handy
I have a script that works mostly the way want. It looks at a cell then compares that to a column in another tab, finds the like items and returns that, and creates a dropdown on the cell. This moves down the column until it reaches the end. The problem is that it continues past the last row for about 20 rows. The starting row is row24.
function getInventoryItems() {
var jobSummaryInventoryItems = jobSummary.getRange(24, 8, jobSummary.getLastRow(), 1);
var jobSummaryInventoryItemsValues = jobSummaryInventoryItems.getValues();
var inventoryItems = inventory.getRange(4, 3, inventory.getLastRow(), 1);
var inventoryItemsValues = inventoryItems.getValues();
jobSummary.getRange(24, 8, jobSummary.getLastRow(), 1).setDataValidation(null);
for (z = 0; z < jobSummaryInventoryItemsValues.length; z++) {
if (jobSummaryInventoryItemsValues[z].toString().length > 1) {
var listOfInventory = [];
for (i = 0; i < inventoryItems.getLastRow() - 4; i++) {
if (inventoryItemsValues[i].toString() == jobSummaryInventoryItemsValues[z]) {
break;
}
var w = jobSummaryInventoryItemsValues[z];
if (inventoryItemsValues[i].toString().includes(jobSummaryInventoryItemsValues[z])) {
listOfInventory.push(inventoryItemsValues[i].toString());
}
}
}
if (listOfInventory.length > 0) {
var rangeRule = SpreadsheetApp.newDataValidation().requireValueInList(listOfInventory).build();
jobSummary.getRange(z + 24, 8).setDataValidation(rangeRule);
}
}
Get Inventory Items
function getInventoryItems() {
const ss = SpreadsheetApp.getActive();
const jobSummary = ss.getSheetByName('Job Summary');
const inventory = ss.getSheetByName('Inventory');
const jobSummaryInventoryItems = jobSummary.getRange(24, 8, jobSummary.getLastRow() - 23, 1);
const jobSummaryInventoryItemsValues = jobSummaryInventoryItems.getValues();
const inventoryItems = inventory.getRange(4, 3, inventory.getLastRow() - 3, 1);
const inventoryItemsValues = inventoryItems.getValues();
jobSummary.getRange(24, 8, jobSummary.getLastRow() -23, 1).setDataValidation(null);
for (z = 0; z < jobSummaryInventoryItemsValues.length; z++) {
if (jobSummaryInventoryItemsValues[z].toString().length > 1) {
let listOfInventory = [];
for (i = 0; i < inventoryItems.length; i++) {
if (inventoryItemsValues[i].toString() == jobSummaryInventoryItemsValues[z]) {
break;
}
let w = jobSummaryInventoryItemsValues[z];
if (inventoryItemsValues[i].toString().includes(jobSummaryInventoryItemsValues[z])) {
listOfInventory.push(inventoryItemsValues[i].toString());
}
}
}
if (listOfInventory.length > 0) {
let rangeRule = SpreadsheetApp.newDataValidation().requireValueInList(listOfInventory).build();
jobSummary.getRange(z + 24, 8).setDataValidation(rangeRule);
}
}
}
Sheet.getRange(row,column,number of rows, number of columns)
I have 13 images shown in page ,one of them is fixed and others changed depend on some data ,i made 12 if statement and it works ,but is there way to create one if statement with for loop like thus :
for (int i = 2; i >= 13; i++)
{
if (m_id >= i)
{
"image" + i.Source = ImageSource.FromFile("months" + i + ".png");
}
else
{
"image" + i.Source = ImageSource.FromFile("months"+i+"closed.png");
}
}
You should put all Images in an Array like
List<Image> Images = new List<Image>() {image2,image3,...,image13 };
And set the ImageSource like
for(int i=2;i<=13;i++)
{
Image image = Images[i - 2];
if (m_id >= i)
{
image.Source = ImageSource.FromFile("months" + i + ".png");
}
else
{
image.Source = ImageSource.FromFile("months"+i+"closed.png");
}
}
Strange phenomenon. socketio
SERVER
i have that code...
socket.on('disconnect', function(){
console.log('user disconnected');
for(var i=0; i<stores.length; i++ ){
var c = stores[i];
if(c.socketid == socket.id){
stores.splice(i,1);
break;
}
}
});
everything goes well with the .splice. If i print the stores array from elsewhere, it displayed correct ...but in this situation
socket.on('disconnect', function(){
console.log('user disconnected');
for(var i=0; i<stores.length; i++ ){
var c = stores[i];
if(c.socketid == socket.id){
for(var i=0; i<stores.length;i++){
console.log(i+" one"+stores[i].name+"-"+stores[i].id)
}
stores.splice(i,1);
for(var i=0; i<stores.length;i++){
console.log(i+" two"+stores[i].name+"-"+stores[i].id)
}
break;
}
}
});
the array has not lost its values, i visit the page from my browser(i connected),
socket.on('storelogged', function (msg){
var storeInfo = new Object();
storeInfo.name = msg.name;
storeInfo.id = msg.id;
storeInfo.socketid = socket.id;
stores.push(storeInfo);
console.log(msg.name + " has connected with " + msg.id + " id." );
});
so, stores pushed. But when i disconnected in the second situation of socket.on('disconnect',callback) the stores array still contains the values( in other words, splice dont work )
comment for giving, much and better information. Also you can test it and see the results
Your embedded for loops are overwriting the i variable from the top level for loop.
Either use let instead of var as in for (let i = 0; ....) for all your for loops so each has a different locally scoped value of i and the inner loops won't overwrite the outer loops or use a different variable name for the embedded for loops or use .forEach() which creates a new variable for the index.
In addition, after you call .splice() on the array you are iterating, you have decrement the current array index from your for loop or you will skip looking at one of the values in the array because the .splice() moved it down into the index spot that you just removed and that your for loop has already iterated.
For example, you can change variable names of the inner for loops like this:
socket.on('disconnect', function () {
console.log('user disconnected');
for (var i = 0; i < stores.length; i++) {
var c = stores[i];
if (c.socketid == socket.id) {
for (var j = 0; j < stores.length; j++) {
console.log(j + " one" + stores[j].name + "-" + stores[j].id)
}
stores.splice(i, 1);
// make sure not to skip the value we just moved into the i slot in the array
i--;
for (var k = 0; i < stores.length; k++) {
console.log(k + " two" + stores[k].name + "-" + stores[k].id)
}
break;
}
}
});
Or, you can use let for the for loops:
socket.on('disconnect', function () {
console.log('user disconnected');
for (var i = 0; i < stores.length; i++) {
var c = stores[i];
if (c.socketid == socket.id) {
for (let i = 0; i < stores.length; i++) {
console.log(i + " one" + stores[i].name + "-" + stores[i].id)
}
stores.splice(i, 1);
// make sure not to skip the value we just moved into the i slot in the array
i--;
for (let i = 0; i < stores.length; i++) {
console.log(i + " two" + stores[i].name + "-" + stores[i].id)
}
break;
}
}
});
Or, you can use .forEach():
socket.on('disconnect', function () {
console.log('user disconnected');
for (var i = 0; i < stores.length; i++) {
var c = stores[i];
if (c.socketid == socket.id) {
stores.forEach(function(item, index) {
console.log(index + " one" + item.name + "-" + item.id)
});
stores.splice(i, 1);
// make sure not to skip the value we just moved into the i slot in the array
i--;
stores.forEach(function(item, index) {
console.log(index + " one" + item.name + "-" + item.id)
});
break;
}
}
});