Spawning a random number of movieclips - function doesn't work? - actionscript-3

I am trying to figure out how to spawn a random number of cells, but every time I run the program, I always end up with one cell only.
How come the number of cells spawned is always 1 when it can go up to 50? (since I made the p variable a random number between 1 and 50).
var myCell:Cell = new Cell();
var minLimit:uint = 1;
var maxLimit:uint = 50;
var range:uint = maxLimit - minLimit;
var p:Number = Math.ceil(Math.random()*range) + minLimit;
for (p; p < maxLimit; p += 1)
{
addChild(myCell);
myCell.x = xp
myCell.y = xp
myCell.scaleX = 6
myCell.scaleY = 6
var xminLimit:uint = 100;
var xmaxLimit:uint = 400;
var xrange:uint = xmaxLimit - xminLimit;
var xp:Number = Math.ceil(Math.random()*xrange) + xminLimit;
}

You need to move the instance creation code (var myCell:Cell = new Cell();) inside the loop to create maxLimit number of instance instead you are creating only one instance outside the loop.
Try this,
var minLimit:uint = 1;
var maxLimit:uint = 50;
var range:uint = maxLimit - minLimit;
var p:Number = Math.ceil(Math.random()*range) + minLimit;
for (p; p < maxLimit; p += 1)
{
var myCell:Cell = new Cell();
var xminLimit:uint = 100;
var xmaxLimit:uint = 400;
var xrange:uint = xmaxLimit - xminLimit;
var xp:Number = Math.ceil(Math.random()*xrange) + xminLimit;
addChild(myCell);
myCell.x = xp
myCell.y = xp
myCell.scaleX = 6
myCell.scaleY = 6
}

Related

How to Get (& Set) cell values from one sheet to another in Google Sheets?

Looking to get values from several cells in the first sheet (POTemplate) of my Google Sheet file that serves as an order entry form. Line 22 is the first line in the form that collects data from our user regarding items requested for order.
The tab to which I'd like to set these values (POHistory) will serve as a running log of all order details keyed into the POTemplate tab with each order. Each entry recorded in the PO template log should include each orders' unique PO No. (found in cell N3 of the POTemplate tab) & PO Date (cell N4 of the POTemplate tab). I sincerely appreciate the help.
function submit() {
var app = SpreadsheetApp;
var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
var tplFRow = 22, tplLRow = tplSheet.getLastRow();
var tplRowsNum = tplLRow - tplFRow + 1;
var tplFCol = 1, tplLCol = 16;
var tplColsNum = tplLCol - tplFCol + 1;
var rangeData = tplSheet.getRange(22, 1, tplRowsNum, tplColsNum).getValues();
var colIndexes = [0, 3, 10, 12, 15];
var fData = filterByIndexes(rangeData, colIndexes);
var target = "POHistory";
var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
var tgtRow = targetSheet.getLastRow() + 1;
var tgtRowsNum = fData.length - tgtRow + 1;
var tgtCol = 1;
var tgtColsNum = fData[0].length - 1 + 1;
targetSheet.getRange(tgtRow, tgtCol, tgtRowsNum,
tgtColsNum).setValues(fData);
}
function filterByIndexes(twoDArr, indexArr) {
var fData = [];
twoDArr = twoDArr.transpose();
for(var i=0; i<indexArr.length; i++) {
fData.push(twoDArr[indexArr[i]]);
}
return fData.transpose();
}
Array.prototype.transpose = function() {
var a = this,
w = a.length ? a.length : 0,
h = a[0] instanceof Array ? a[0].length : 0;
if (h === 0 || w === 0) {return [];}
var i, j, t = [];
for (i = 0; i < h; i++) {
t[i] = [];
for (j = 0; j < w; j++) {
t[i][j] = a[j][i];
}
}
return t;
}
I can offer you a modification of the submit() function you have given.
function process(){
submit();
submitDate();
}
function submitDate() {
var app = SpreadsheetApp;
var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
var tplFRow = 1, tplLRow = 21;
var tplRowsNum = tplLRow - tplFRow + 1;
var tplFCol = 1, tplLCol = 16;
var tplColsNum = tplLCol - tplFCol + 1;
var rangeData = tplSheet.getRange(tplFRow, tplFCol, tplLRow, tplColsNum).getValues();
var colIndexes = [13];
var fData = filterByIndexes(rangeData, colIndexes);
var target = "POHistory";
var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
var tgtRow = targetSheet.getLastRow() + 1;
var tgtRowsNum = fData.length - tgtRow + 1;
var tgtCol = 1;
var tgtColsNum = fData[0].length - 1 + 1;
Logger.log(fData)
fData=fData.transpose();
Logger.log(fData)
targetSheet.getRange(tgtRow-1, 5, fData.length,
fData[0].length).setValues(fData);
}
function submit() {
var app = SpreadsheetApp;
var tplSheet = app.getActiveSpreadsheet().getSheetByName("POTemplate");
var tplFRow = 22, tplLRow = tplSheet.getLastRow();
var tplRowsNum = tplLRow - tplFRow + 1;
var tplFCol = 1, tplLCol = 16;
var tplColsNum = tplLCol - tplFCol + 1;
var rangeData = tplSheet.getRange(22, 1, tplRowsNum, tplColsNum).getValues();
var colIndexes = [0, 3, 10, 12, 15];
var fData = filterByIndexes(rangeData, colIndexes);
var target = "POHistory";
var targetSheet = app.getActiveSpreadsheet().getSheetByName(target);
var tgtRow = targetSheet.getLastRow() + 1;
var tgtRowsNum = fData.length - tgtRow + 1;
var tgtCol = 1;
var tgtColsNum = fData[0].length - 1 + 1;
targetSheet.getRange(tgtRow, tgtCol, tgtRowsNum,
tgtColsNum).setValues(fData);
}
function filterByIndexes(twoDArr, indexArr) {
var fData = [];
twoDArr = twoDArr.transpose();
for(var i=0; i<indexArr.length; i++) {
fData.push(twoDArr[indexArr[i]]);
}
return fData.transpose();
}
Array.prototype.transpose = function() {
var a = this,
w = a.length ? a.length : 0,
h = a[0] instanceof Array ? a[0].length : 0;
if (h === 0 || w === 0) {return [];}
var i, j, t = [];
for (i = 0; i < h; i++) {
t[i] = [];
for (j = 0; j < w; j++) {
t[i][j] = a[j][i];
}
}
return t;
}
Basically, similar code runs twice. I could be criticized for doing this, but perhaps it is convenient for you this way.

export all rows of a html table with pagenation to excel sheet

I have one html table where pagenation is used. So it displays 10 rows once. When i am exporting my table to a excel, only the first 10 rows which are there in the html table are getting exported. I want all the rows to be exported. Please suggest some way.
Below is the code I am using.
function exportToExcel(){
var mytable = document.getElementById("tlb_setup");
var rowCount = mytable.rows.length;
var colCount = mytable.getElementsByTagName("tr")[0].getElementsByTagName("th").length;
var ExcelApp = new ActiveXObject("Excel.Application");
ExcelApp.Workbooks.Open("........\\requestsInExcel.xlsx");
var ExcelSheet = new ActiveXObject("Excel.Sheet");
ExcelApp.Visible = true;
//var ExcelSheet = ExcelApp.Worksheets("Sheet1");
//ExcelSheet.Application.Visible = true;
for (var i = 0; i < rowCount; i++) {
for (var j = 0; j < colCount; j++) {
if (i == 0) {
str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("th")[j].innerText;
}
else {
str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerText;
}
ExcelApp.ActiveSheet.Cells(i + 1, j + 1).Value = str;
//Workbook.SaveAs("........\\requestsInExcel.xlsx");
}
}
//ExcelApp.ActiveWorkbook.Save();
ExcelApp.ActiveSheet.columns.autofit;
ExcelSheet.Application.Visible = true;
DisplayAlerts = true;
}

Losing importXML functions when running my current script

I am writing a Google script to copy values from a column of importXML data based on a certain date. Below is where I am currently, but the issue I am running into now is when the script is run it is removing the importXML formulas located in columnns G and H:
function moveValuesOnly2() {
var sheet = SpreadsheetApp.openById("0At2fAZApHI8adHJxWU5EVVBEbHd0YzA5UVBwOGQ3ZHc");
SpreadsheetApp.setActiveSpreadsheet(sheet);
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('Dylan'))
var range= sheet.getRange("B3:AJ50");
var currentValue = range.getValues();
var COL_B = 0; // relative to col B
var COL_G = 5;
var COL_H = 6;
var COL_I = 7;
var COL_J = 8;
var COL_K = 9;
var COL_L = 10;
var COL_N = 12;
var COL_O = 13;
var COL_P = 14;
var COL_R = 16;
var COL_S = 17;
var COL_T = 18;
var COL_V = 20;
var COL_W = 21;
var COL_X = 22;
var COL_Z = 24;
var COL_AA = 25;
var COL_AB = 26;
var COL_AD = 28;
var COL_AE = 29;
var COL_AF = 30;
var COL_AH = 32;
var COL_AI = 33;
var COL_AJ = 34;
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (1)) {
// Copy value at 0 days
currentValue[i][COL_J] = currentValue[i][COL_G];
currentValue[i][COL_K] = currentValue[i][COL_H];
currentValue[i][COL_L] = currentValue[i][COL_I];
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (15)) {
// Copy value at 15 days
currentValue[i][COL_N] = currentValue[i][COL_G];
currentValue[i][COL_O] = currentValue[i][COL_H];
currentValue[i][COL_P] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (30)) {
// Copy value at 30 days
currentValue[i][COL_R] = currentValue[i][COL_G];
currentValue[i][COL_S] = currentValue[i][COL_H];
currentValue[i][COL_T] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (45)) {
// Copy value at 45 days
currentValue[i][COL_V] = currentValue[i][COL_G];
currentValue[i][COL_W] = currentValue[i][COL_H];
currentValue[i][COL_X] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (60)) {
// Copy value at 60 days
currentValue[i][COL_Z] = currentValue[i][COL_G];
currentValue[i][COL_AA] = currentValue[i][COL_H];
currentValue[i][COL_AB] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (75)) {
// Copy value at 75 days
currentValue[i][COL_AD] = currentValue[i][COL_G];
currentValue[i][COL_AE] = currentValue[i][COL_H];
currentValue[i][COL_AF] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}
for(var i = 0 ; i < currentValue.length ; i++){
if (currentValue[i][0] == (90)) {
// Copy value at 90 days
currentValue[i][COL_AH] = currentValue[i][COL_G];
currentValue[i][COL_AI] = currentValue[i][COL_H];
currentValue[i][COL_AJ] = currentValue[i][COL_I];
}
range.setValues(currentValue);
}}}
Is there any way to prevent the ImportXML functions located in those columns from being lost each time the script is run. Also a huge thank you to user #Srik to getting to me to the point I am at!
I would think that the problem lies from the fact that you setValues on the range which includes your columns G & H.
The solution lies in using
var specialRange = sheet.getRange("G3:H50");
var formulas = specialRange.getFormulas();
at the begining of your script to save the formulas and then after your setValues call to use the following code
specialRange.setFormulas(formulas);
Let me know how that works for you.

Syntax error 1084 whatever i do

I am trying to make a tool for a game...
Pushing a button gives some results after calculations. The Code is not finished yet but it does not have to give errors. Here is the code:
import flash.events.MouseEvent;
//-----------------------variables------------------------
var iPPP1:String;
var iPPP2:String;
var iPPP3:String;
var iPPP4:String;
var iPPP5:String;
var iPPP6:String;
var iPPP7:String;
var iCntbonus:String;
var iRawPrice:String;
var iAverSal:String;
var iTax:String;
var iNoC7:String;
var iNoC6:String;
var iNoC5:String;
var iNoC4:String;
var iNoC3:String;
var iNoC2:String;
var iNoC1:String;
var iEm7:String;
var iEm6:String;
var iEm5:String;
var iEm4:String;
var iEm3:String;
var iEm2:String;
var iEm1:String;
var iRaw5:String;
var iRaw4:String;
var iRaw3:String;
var iRaw2:String;
var iRaw1:String;
var apotelesma:Number;
var bonus:Number;
var i1:Number;
var i2:Number;
var i3:Number;
var i4:Number;
var i5:Number;
var i6:Number;
var i7:Number;
//------------------------restricts------------------------
PPP1.restrict = "0-9\\.";
PPP2.restrict = "0-9\\.";
PPP3.restrict = "0-9\\.";
PPP4.restrict = "0-9\\.";
PPP5.restrict = "0-9\\.";
PPP6.restrict = "0-9\\.";
PPP7.restrict = "0-9\\.";
Cntbonus.restrict = "0-5";
RawPrice.restrict = "0-9\\.";
AverSal.restrict = "0-9\\.";
Tax.restrict = "0-9";
NoC7.restrict = "0-9";
NoC6.restrict = "0-9";
NoC5.restrict = "0-9";
NoC4.restrict = "0-9";
NoC3.restrict = "0-9";
NoC2.restrict = "0-9";
NoC1.restrict = "0-9";
Em7.restrict = "0-9";
Em6.restrict = "0-9";
Em5.restrict = "0-9";
Em4.restrict = "0-9";
Em3.restrict = "0-9";
Em2.restrict = "0-9";
Em1.restrict = "0-9";
Raw5.restrict = "0-9";
Raw4.restrict = "0-9";
Raw3.restrict = "0-9";
Raw2.restrict = "0-9";
Raw1.restrict = "0-9";
//-------------------------------borders----------------------------
PPP1.border = true;
PPP2.border = true;
PPP3.border = true;
PPP4.border = true;
PPP5.border = true;
PPP6.border = true;
PPP7.border = true;
Cntbonus.border = true;
RawPrice.border = true;
AverSal.border = true;
Tax.border = true;
NoC7.border = true;
NoC6.border = true;
NoC5.border = true;
NoC4.border = true;
NoC3.border = true;
NoC2.border = true;
NoC1.border = true;
Em7.border = true;
Em6.border = true;
Em5.border = true;
Em4.border = true;
Em3.border = true;
Em2.border = true;
Em1.border = true;
Raw5.border = true;
Raw4.border = true;
Raw3.border = true;
Raw2.border = true;
Raw1.border = true;
//--------------------------calculations-------------------------------
calc_btn.addEventListener(MouseEvent.CLICK, Calco);
function Calco(event:MouseEvent):void;
{
iPPP1 = PPP1.text;
iPPP2 = PPP2.text;
iPPP3 = PPP3.text;
iPPP4 = PPP4.text;
iPPP5 = PPP5.text;
iPPP6 = PPP6.text;
iPPP7 = PPP7.text;
iCntbonus = Cntbonus.text;
iRawPrice = RawPrice.text;
iAverSal = AverSal.text;
iTax = Tax.text;
iNoC7 = NoC7.text;
iNoC6 = NoC6.text;
iNoC5 = NoC5.text;
iNoC4 = NoC4.text;
iNoC3 = NoC3.text;
iNoC2 = NoC2.text;
iNoC1 = NoC1.text;
iEm7 = Em7.text;
iEm6 = Em6.text;
iEm5 = Em5.text;
iEm4 = Em4.text;
iEm3 = Em3.text;
iEm2 = Em2.text;
iEm1 = Em1.text;
iRaw5 = Raw5.text;
iRaw4 = Raw4.text;
iRaw3 = Raw3.text;
iRaw2 = Raw2.text;
iRaw1 = Raw1.text;
i1 = (parseInt(iEm1) + parseInt(iNoC1)) * 10 * bonus;
i2 = (parseInt(iEm2) + parseInt(iNoC2)) * 10 * bonus;
i3 = (parseInt(iEm3) + parseInt(iNoC3)) * 10 * bonus;
i4 = (parseInt(iEm4) + parseInt(iNoC4)) * 10 * bonus;
i5 = (parseInt(iEm5) + parseInt(iNoC5)) * 10 * bonus;
i6 = (parseInt(iEm6) + parseInt(iNoC6)) * 10 * bonus;
i7 = (parseInt(iEm7) + parseInt(iNoC7)) * 10 * bonus;
if (parseInt(iCntbonus) == 0) {
bonus = 1;
} else if (parseInt(iCntbonus) == 1) {
bonus = 1,2;
} else if (parseInt(iCntbonus) == 2) {
bonus = 1,4;
} else if (parseInt(iCntbonus) == 3) {
bonus = 1,6;
} else if (parseInt(iCntbonus) == 4) {
bonus = 1,8;
} else {
bonus = 2;
}
// υπολογισμός εσόδων
apotelesma = (Number(iRawPrice)*bonus*parseInt(iRaw1)*35)+(Number(iRawPrice)*bonus*parseInt(iRaw2)*70)+(Number(iRawPrice)*bonus*parseInt(iRaw3)*125)+(Number(iRawPrice)*bonus*parseInt(iRaw4)*175)+(Number(iRawPrice)*bonus*parseInt(iRaw5)*250)
apotelesma = apotelesma + (i1 * Number(iPPP1)) - (i1 * (parseInt(iTax) / 100) + (i2 * Number(iPPP2)) - (i2 * (parseInt(iTax) / 100) + (i3 * Number(iPPP3)) - (i3 * (parseInt(iTax) / 100) + (i4 * Number(iPPP4)) - (i4 * (parseInt(iTax) / 100) + (i5 * Number(iPPP5)) - (i5 * (parseInt(iTax) / 100) + (i6 * Number(iPPP6)) - (i6 * (parseInt(iTax) / 100) + (i7 * Number(iPPP7)) - (i7 * (parseInt(iTax) / 100)
apotelesma.toString()
}
I have problem with the last 3 lines...
You have some very long calculations, and are missing atleast 5 closing parenetheses. I pasted the 2nd to the last line (the one that starts with apotelesma = apotelesma) into a text editor and counted the numbers of opening/closing parentheses.
The calculation is so long, it's not immediately clear where they should be inserted. I would break down the calculations so that they are more readable. Anyone maintaining this code after you will benefit from the clarity.
Either break the long calculations up into many smaller ones, or just add line breaks in the code so it's clear what you're trying to do, like this:
apotelesma = apotelesma +
(i1 * Number(iPPP1)) -
(i1 * (parseInt(iTax) / 100) + // NOTE: you seem to be missing a closing paren here
(i2 * Number(iPPP2)) -
(i2 * (parseInt(iTax) / 100) + // and here... (and so on)
...

Cannot see the content inside of a MovieClip

I don't know why this is happening. What I want to do is easy: Create a container and then add a grid of squares in order to build a trivia:
private var square:MovieClip = new MovieClip();
square.width = 308;
square.height = 400;
square.x = 48;
square.y = 223;
square.name = "square";
addChild(square);
private function generarGrilla():void {
var cant = 36;
var col:Number = 5;
var yCounter:Number = -4;
var xCounter:Number = 4;
var sY:Number = 10;
var sX:Number = 10;
for (var j = 1; j < cant; j++) {
var caja:clip = new clip();
caja.name = "opcion" + j;
caja.x = 20 + caja.width * j * 1.2;
caja.y = 20 + caja.height * j * 1.2;;
// caja.x = (sX + caja.width) * xCounter ;
// caja.y = (sY + caja.height) * yCounter;
caja.addEventListener(MouseEvent.CLICK, seleccionarOpcion);
caja.buttonMode = true;
caja.mouseChildren = false;
var contentText = new TextField();
var formato = new TextFormat();
formato.size = 14;
contentText.defaultTextFormat = formato;
contentText.width = 36;
contentText.height = 34;
contentText.x = -10;
contentText.y = -10;
for(var u:uint = 0; u < cant; u++){
contentText.text = "" + u;
}
square.addChild(caja);
caja.addChild(contentText);
}
var barra:score = new score();
barra.x = 80;
barra.y = -200;
barra.puntajeTXT.text = "hola";
addChild(barra);
barra.botonSalir.buttonMode = true;
barra.botonSalir.addEventListener(MouseEvent.CLICK, salirJuego);
}
The square movieclip is placed on the stage without any problem, but I cannot see the grid...