I have a script that is working. It is to loop thru a number of different spreadsheets, and make the same edits in each. The edits are: Adding 2 formulas, conditional formatting a cell, and removing protection from 6 sheets. But there seems to be an issue with the first 'FOR' loop. It is only doing 2 iterations, then keeps running. I have had to force stop each of my attempts after several minutes. I am fairly new, so I think the issue may be with my syntax and brackets {}.
var sheetIds = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Blueprints"); //Change as needed -DONE
var values = sheetIds.getRange('j19:j96').getValues(); //Change to proper range -DONE j3:j96
var idList = values.map(function (row) {
return row[0];
}).filter(function (id) {
return id;
});
for (var i = 0; i < idList.length; i++) {
// var form1 = "Roster!A1"
// var form2 = "KPI!A1"
var s = SpreadsheetApp.openById(idList[i]);
var ss = s.getSheetByName('Blueprint');
var roster = ss.getRange('e3');
var kpi = ss.getRange('f3');
var form1 = "Roster!A1"
var form2 = "KPI!A1"
roster.setFormula(form1)
kpi.setFormula(form2)
//var range = ss.getRange('t1')
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenTextContains('Advocates')
.setFontColor('#c9daf8')
.setRanges([roster])
.build();
var rules = ss.getConditionalFormatRules();
rules.push(rule);
ss.setConditionalFormatRules(rules);
//var s = SpreadsheetApp.getActiveSpreadsheet();
var store = s.getSheetByName('Store');
var prot = store.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot.length; i++) {
var protection = prot[i];
if (protection.canEdit()) {
protection.remove();
}
}
var adv1 = s.getSheetByName('Advocate1');
var prot1 = adv1.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot1.length; i++) {
var protection1 = prot1[i];
if (protection1.canEdit()) {
protection1.remove();
}
}
var adv2 = s.getSheetByName('Advocate2');
var prot2 = adv2.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot2.length; i++) {
var protection2 = prot2[i];
if (protection2.canEdit()) {
protection2.remove();
}
}
var adv3 = s.getSheetByName('Advocate3');
var prot3 = adv3.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot3.length; i++) {
var protection3 = prot3[i];
if (protection3.canEdit()) {
protection3.remove();
}
}
var adv4 = s.getSheetByName('Advocate4');
var prot4 = adv4.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot4.length; i++) {
var protection4 = prot4[i];
if (protection4.canEdit()) {
protection4.remove();
}
}
var adv5 = s.getSheetByName('Advocate5');
var prot5 = adv5.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var i = 0; i < prot5.length; i++) {
var protection5 = prot5[i];
if (protection5.canEdit()) {
protection5.remove();
}
}
}
}
It would be a massive time saver if I could figure this out!!!
Try this:
I just replaced all of the inner loop indices with j
function myFunction() {
var sheetIds=SpreadsheetApp.getActive.getSheetByName("Blueprints");
var values=sheetIds.getRange('j19:j96').getValues();
var idList=values.map(function (row) {return row[0];}).filter(function (id) {return id;});
for (var i=0;i<idList.length;i++) {
var s=SpreadsheetApp.openById(idList[i]);
var ss=s.getSheetByName('Blueprint');
var roster=ss.getRange('e3');
var kpi=ss.getRange('f3');
var form1="Roster!A1";
var form2="KPI!A1";
roster.setFormula(form1);
kpi.setFormula(form2);
var rule=SpreadsheetApp.newConditionalFormatRule().whenTextContains('Advocates').setFontColor('#c9daf8').setRanges([roster]).build();
var rules=ss.getConditionalFormatRules();
rules.push(rule);
ss.setConditionalFormatRules(rules);
var store=s.getSheetByName('Store');
var prot=store.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for(var j=0;j<prot.length; j++) {
var protection=prot[j];
if (protection.canEdit()) {
protection.remove();
}
}
var adv1=s.getSheetByName('Advocate1');
var prot1=adv1.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var j=0; j < prot1.length; j++) {
var protection1=prot1[j];
if (protection1.canEdit()) {
protection1.remove();
}
}
var adv2=s.getSheetByName('Advocate2');
var prot2=adv2.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var j=0; j < prot2.length; j++) {
var protection2=prot2[j];
if (protection2.canEdit()) {
protection2.remove();
}
}
var adv3=s.getSheetByName('Advocate3');
var prot3=adv3.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var j=0; j < prot3.length; j++) {
var protection3=prot3[j];
if (protection3.canEdit()) {
protection3.remove();
}
}
var adv4=s.getSheetByName('Advocate4');
var prot4=adv4.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var j=0; j < prot4.length; j++) {
var protection4=prot4[j];
if (protection4.canEdit()) {
protection4.remove();
}
}
var adv5=s.getSheetByName('Advocate5');
var prot5=adv5.getProtections(SpreadsheetApp.ProtectionType.SHEET)
for (var j=0; j < prot5.length; j++) {
var protection5=prot5[j];
if (protection5.canEdit()) {
protection5.remove();
}
}
}
}
Related
I found the snippet below here: Re-set checkboxes to false - Google Apps Script
I'm interested in editing this to set false checkboxes to true, specifically adding to it to skip blank cells. Can't find anything helpful on skipping blanks.
function resetCheckBoxesAllSheets() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var dataRange = sheet.getRange('A4:Z100');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == true) {
values[i][j] = false;
}
}
}
dataRange.setValues(values);
}
}
If you wish to flip flop the values of a set of checkboxes this will do it for a given column active range:
Note: capitalization counts and yes those are strings. If you have any doubts about it then use your script editor's debugger to see what is in your checkboxes. That's how I figured it out when I started playing with them.
function resetCheckBoxesAllSheets() {
var ss = SpreadsheetApp.getActive();
var sheet=ss.getActiveSheet()
var dataRange = sheet.getActiveRange();
var values = dataRange.getValues();
for (var i=0;i<values.length;i++) {
values[i][0]=values[i][0]?"FALSE":"TRUE";
}
dataRange.setValues(values);
}
So in your particular case, assuming everything else in your function works then try this:
function resetCheckBoxesAllSheets() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var s in allsheets){
var sheet=allsheets[s]
var dataRange = sheet.getRange('A4:Z100');
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
if (values[i][j]) {
values[i][j] = "FALSE";
}
}
}
dataRange.setValues(values);
}
}
I have an api 'rest/latest/testruns/16543558' the id is test run ids. I want to update a call with different ids in one shot . I have tried with $promise.all(). Its working fine if I give an individual ID but if I give more than one id its giving error
With single ID Working FINE :
var ids = ['16611544']; //'16611345','16611347'
$scope.updateTestRun = function(data) {
data.showedit = true;
$scope.label = 'Save';
var updateform = {};
var updateArr = [];
for (var i = 0; i < data.length; i++) {
var testrunid = data[i].data.data.id;
var updatedata = data[i].data.data.fields;
updateform['fields'] = data[i].data.data.fields;
updateform.fields['duration'] = 1000;
delete updateform.fields['executionDate'];
for (var j = 0; j < data[i].data.data.fields.testRunSteps.length; j++) {
data[i].data.data.fields.testRunSteps[j].status = data[i].data.data.fields.testRunStatus;
}
updateArr.push($http.put("rest/latest/testruns/" + testrunid, updateform))
j = 0;
$scope.loader = true;
}
}
u can use $q.all to send multiple request on one shot
var fullArray = [];
for (var i = 0; i < data.length; i++) {
var testrunid = data[i].data.data.id;
var updatedata = data[i].data.data.fields;
updateform['fields'] = data[i].data.data.fields;
updateform.fields['duration'] = 1000;
delete updateform.fields['executionDate'];
for (var j = 0; j < data[i].data.data.fields.testRunSteps.length; j++) {
data[i].data.data.fields.testRunSteps[j].status = data[i].data.data.fields.testRunStatus;
}
j = 0;
$scope.loader = true;
// push all request to array
fullArray.push($http.put("rest/latest/testruns/" + testrunid, updateform))
}
$q.all(fullArray).then(function(values) {
//resoponse
});
I have this code on Google script for get arrays from Sheet1 by criteria in Sheet2 at Sheet3. But now arrays placed only one under the other. What I need is place every new array from 'v' in next 5 columns like in example on my spreadsheet.
Secondly - before this, I used filter with search formula, that allow me use wildcards like * or ?. How I can use wildcards or regexp in my new function?
I would be grateful for any help.
function getval(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var sspodbor = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet3");
var range = ss.getRange("A2:A29");
var values = range.getValues();
var rangez = sheet.getRange("A1:A14");
var valuesz = rangez.getValues();
var z = []
for (var x = 0; x<valuesz.length; x++){
z.push(valuesz[x])
}
var v = [];
for (var q = 0; q < valuesz.length; q++){
for (var s = 0; s < values.length; s++){
if(values[s][5] == z[q]){
v.push([values[s][0],values[s][1],values[s][2],values[s][3],values[s][4]]);
}
//I am guessing that here must be a separating function
}
}
var range = sspodbor.getRange(4, 1, v.length,v[0].length);
range.setValues(v);
}
My spreadsheet: https://docs.google.com/spreadsheets/d/1o7ErbeFHA7yyxMC0HMn3Uj5ZBRcy2uAwa1UpolVpBFI/edit?usp=sharing
Spreading the Groups out Horizontally
It's not the prettiest solution you'll ever see and hopefully others will look it over and make improvements but here it is.
function getval()
{
var Sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var Sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2");
var output = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("output");
var knew = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('knew');
var knewrange = knew.getRange(1,1,10,100);
var pipe = '';
var range1 = Sheet1.getRange("A2:F29");
var values1 = range1.getValues();
var range2 = Sheet2.getRange("A1:A14");
var values2 = range2.getValues();
var z = [];
for (var x = 0; x<values2.length; x++)
{
z.push(values2[x])
}
var v = [];
for (var q = 0; q < values2.length; q++)
{
for (var s = 0; s < values1.length; s++)
{
if(values1[s][5] == z[q])
{
v.push([values1[s][0],values1[s][1],values1[s][2],values1[s][3],values1[s][4],q]);
}
}
}
var vlength=v.length;
var range3 = output.getRange(1, 1, v.length,6);
range3.setValues(v);
var w = [];
var voff = 0;
var hoff = 0;
for(var m=0;m<10;m++)
{
w[m]=[];
for(var n=0;n<100;n++)
{
w[m][n]='' ;
}
}
var color = ['yellow','orange'];
for(var i=0;i<v.length;i++)
{
for(var j=0;j<5;j++)
{
if(i-voff==0){knew.getRange((i-voff + 1),(j + hoff + 1),4,5).setBackground(color[v[i][5] % 2])};
if((i-voff)==0 || v[i][5] == v[i-1][5])
{
w[i - voff][j + hoff]=v[i][j];
}
else
{
voff = i;
hoff += 5;
w[i - voff][j + hoff]=v[i][j];
}
}
}
knewrange.setValues(w);
}
I copied the data from your spreadsheet and the the original getval function. I ended up changing some of the names so I could figure out where to find the data easier. It was a difficult problem for me and one that i enjoyed.
Thanks
I am trying to reset a scene an move every thing to its original position the reset function resets the array adds the nape bodies back to the stage and attaches the graphics but the original graphics still are on the stage in whatever position they were in when reset was called
private var brickGraphic:MovieClip = new Brick();
private var brick:Body;
private var brickArray:Array;
private function setUp():void
{
brickArray = new Array ;
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
private function reset():void
{
if (contains(brickGraphic)) removeChild(brickGraphic);
space.clear();
setUp();
}
}
this is the final issue i am having on this app and your help would be greatly appreciated
That's because you are not removing them with removeChild.
You need to call removeChild for each brickGraphic object you add to the stage.
Something like :
private function setUp():void
{
brickArray = [];
for (var i:int = 0; i < 10; i++)
{
var brick:Body = new Body(BodyType.DYNAMIC);
var brickShape:Polygon = new Polygon(Polygon.box(10,25));
var brickGraphic = new Brick();
brickGraphic.width = 10;
brickGraphic.height = 25;
addChild(brickGraphic);
brickGraphic.cacheAsBitmap = true;
brick.shapes.add(brickShape);
brick.position.setxy(450, ((ag ) - 30 * (i + 0.5)));
brick.angularVel = 0;
brick.shapes.at(0).material.elasticity = .5;
brick.shapes.at(0).material.density = 150;
brick.cbTypes.add(brickType);
brick.space = space;
brickGraphic.stop();
brick.userData.sprite = brickGraphic;
brick.userData.sprite.x = brick.position.x;
this.brickArray.push(brick);
}
}
private function removeAllBricks():void
{
for(var i:int=0; i<brickArray.length; i++)
{
var dp:DisplayObject = brickArray[i].userData.sprite as DisplayObject;
if(dp && dp.parent)
dp.parent.removeChild(dp);
}
}
private function reset():void
{
removeAllBricks();
space.clear();
setUp();
}
I'm wondering which is faster in AS3:
array.forEach( function(v:Object, ...args):void
{ ... } );
Or
var l:int = array.length;
for ( var i:int = 0; i < l; i++ ) { ... }
for i :)
var array:Array = [];
for (var i:int=0; i < 100000; i++)
{
array[i] = i;
}
var time:uint = getTimer();
array.forEach( function(v:Object, ...args):void
{ v = 1; } );
trace(getTimer()-time); //trace 85
time = getTimer();
var l:int = array.length;
for ( i = 0; i < l; i++ ) { array[i] = 0; }
trace(getTimer()-time); //trace 3
The above answers do not take into account that you mostly will be performing operations on the array elements in the loop
This code
import flash.utils.getTimer;
var a:Array=[];
var time:int=0;
for(var i:int=0; i<10000; i++) {
a.push(new Date());
}
time=getTimer();
for(var j:int=0; j<a.length; j++) {
var dt:Date=a[j];
dt.valueOf();
}
trace("for: " , getTimer()-time);
time=getTimer();
for each(var xt:Date in a) {
xt.valueOf();
}
trace("for each: " , getTimer()-time);
time=getTimer();
a.forEach(nothing);
trace("a.forEach: " , getTimer()-time);
function nothing(d:Date, ...args):void {
d.valueOf();
}
Returns the following:
for: 3
for each: 2
a.forEach: 13
For 100000 values, the results are starker still
for: 27
for each: 17
a.forEach: 138
Overall winner: the for each loop
for each(var d:myClass in myCollection) {
//myCode
}
For VS Foreach on Array performance (in AS3/Flex)
hope this will help you in understanding the difference between for and for-each loop.
var time:uint;
var vec:Vector.<Number> = new Vector.<Number>;
for (var b:int=0; b < 1000000; b++)
{
vec[b] = 99;
}
///
time = getTimer();
for (var i:int = 0; i < vec.length; i++ )
{
vec[i] = 2;
}
trace('for i: '+(getTimer()-time)+'ms');
///
time = getTimer();
for (var j:int = vec.length - 1; j >= 0; --j)
{
vec[j] = 3;
}
trace('for i rev: '+(getTimer()-time)+'ms');
///
time = getTimer();
for ( var k:int in vec)
{
vec[k] = 4;
}
trace('for: '+(getTimer()-time)+'ms');
///
time = getTimer();
for each(var o:Number in vec)
{
o = 5;
}
trace('for each: '+(getTimer()-time)+'ms');
///
time = getTimer();
vec.forEach( function(v:int, ...args):void
{
v = 6;
}
);
trace('forEach: '+(getTimer()-time)+'ms');
// for i: 81ms
// for i rev: 79ms
// for: 28ms
// for each: 33ms
// forEach: 530ms