Javascript illustrator loop not working - javascript

I am trying to send pathitems to the back of the layer via their selection order, but for some reason as the loop iterates though the selection[i] index it doesn't return them in order
var openDoc = app.activeDocument;
for (var i = 0; i < 3; i++){
var topObject = app.activeDocument.selection[i];
var activelayer = topObject.zOrder(ZOrderMethod.SENDTOBACK);
}
but if I try to unfold the loop it works:
var openDoc = app.activeDocument;
var topObject = app.activeDocument.selection[0];
var activelayer = topObject.zOrder(ZOrderMethod.SENDTOBACK);
var topObject2 = app.activeDocument.selection[1];
var activelayer = topObject.zOrder(ZOrderMethod.SENDTOBACK);
What am i doing wrong?

Related

Google Ad Script

I have a Google Ads script which pulls close variant keyword performance if it is above a ROAS threshold, and then exports it to a google sheet every 30 days. I'm then looking for a way for the script to take the keyword and ad group data and loop through the sheets, lookup the keywords and return the quality scores for these keywords after I have added them.
Here's my below code, I'm struggling with adding to a 2D array from the Google sheet (as you need a 2d array to look up keywords). I also need to be able to push the data back to the sheet. Any thoughts please?
var sheets = SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets();
for (i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var data_range = sheet.getDataRange();
var last_row = data_range.getLastRow();
for(var r=2;r<=last_row;r++) {
var list = [];
var keywordID = [];
var adGroupId = [];
var keywordID = data_range.getCell(r,13).getValue();
var adGroupId = data_range.getCell(r,14).getValue();
list.push([adGroupId,keywordID]);
var keywords = AdsApp.keywords().withIds(list).get();
while (keywords.hasNext()) {
var keyword = keywords.next();
var stats = [];
var stats = keyword.getQualityScore();
row_array.push(stats);
sheet.appendRow(row_array)
}
}
}
var data_range = sheet.getDataRange(); is your 2D range
var data = sheet.getDataRange().getValues(); is your 2D array
You can loop through the data this way (no need the last_row):
for(var r=2; r<=data.length; r++)
Then
var keywordID = data[r][13]; // or [12], I don't know
var adGroupId = data[r][14]; // or [13]
Probably it would look about like this
var sheets = SpreadsheetApp.openByUrl(spreadsheetUrl).getSheets();
for (i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var data = sheet.getDataRange().getValues();
for (var r = 2; r <= data.length; r++) {
var keywordID = data[r][13]; // or [12]
var adGroupId = data[r][14]; // or [13]
var keywords = AdsApp.keywords().withIds([adGroupId, keywordID]).get(); // ?
var row_array = []
while (keywords.hasNext()) {
var stats = keywords.next().getQualityScore();
row_array.push(stats);
}
sheet.appendRow(row_array);
}
}
I haven't tested it since I have no your data and not quite understand the task.

iterate through rows if x push to another spreadsheet

I have the script mostly working- except the data being pushed actually says "Range" I must be missing something- can you not set values across a range?
function up4Grabs() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var destsheet = SpreadsheetApp.openById("MYID GOES HERE").getSheetByName('Items');
var destLastRow = destsheet.getLastRow();
var destRange = destsheet.getRange(1,9,destLastRow);
var dataLastRow = sheet1.getLastRow();
var dataRange = sheet1.getRange(1,9,dataLastRow);
var data = dataRange.getValues();
for(var i = 0; i < data.length; i++) {
if (data[i] > 0) {
var targetLastRow = destsheet.getLastRow() + 1;
var test = destsheet.getRange(1,9,targetLastRow);
sheet1.getvalues(test).setValues(sheet1.getRange(i+1,1,1,9))
}
}
}
Problem:
setValue() setting "Range" rather than actual values.
Cause:
You're passing a range rather than a value to the setValue() in the first place.
Solution:
function up4Grabs() {
var sheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Final');
var destsheet = SpreadsheetApp.openById("MY ID GOES HERE").getSheetByName('Items');
var destLastRow = destsheet.getLastRow();
var destRange = destsheet.getRange(1,9,destLastRow);
var dataLastRow = sheet1.getLastRow();
var dataRange = sheet1.getRange(1,9,dataLastRow);
var data = dataRange.getValues();
for(var i = 0; i < data.length; i++) {
if (data[i] > 0) {
var targetLastRow = destsheet.getLastRow() + 1;
var values = sheet1.getRange(i+1,1,1,9).getValues();
destsheet.getRange(1,9,values.length,9).setValues(values);
}
}
}
Since you're setting the values of a whole range, you need to use setValues() rather than setValue(), note: plural rather than singular. I've also added getValues() to the range you were already pulling, this returns the array of values within the range that can then be passed to setValues().

Why is my for loop running an extra iteration and not copying to the correct cells

When I run my (messy) script it seems to run one extra time than required. i.e. below the last row it creates a pdf and marks a cell as processed.
The second issue is that the URLs don't seem to line up correctly with the name that it should be.
I appreciate the code is very messy but I'm happy to explain the reasoning for any part if it doesn't make sense.
Thanks in advance for any help!
The spreadsheet can be found here:
https://docs.google.com/spreadsheets/d/1nq5RRcwAKk9sFm6jVypFT_qJWcOGxFDZRv6ZpB-QClw/edit#gid=1194576382
and the code that's not working as expected is:
var ss = SpreadsheetApp.getActiveSpreadsheet()
var rawData = "rawData"
var practicePivot = "practicePivot"
var querySheet = "querySheet"
var pdfSheet = "pdfSheet"
var contactList = "contactList"
function createPDF(){
var sourceSheet = ss.getSheetByName("querySheet")
var pdfList = ss.getSheetByName("practicePivot")
var contactList = ss.getSheetByName("contactList")
var sourceRow = sourceSheet.getLastRow()
var sourceColumn = sourceSheet.getLastColumn()
var sourceStartRow = 4 //skips the headers and only pulls query data
var sourceStartColumn = 1
var sourceRange = sourceSheet.getRange(sourceStartRow, sourceStartColumn, sourceRow, sourceColumn)
var sourceValues = sourceRange.getValues()
var pdfLastRow = pdfList.getLastRow()
var storePracticeName = sourceSheet.getRange("A2").getValues()
var newSpreadsheet = SpreadsheetApp.create("Summary of Patients for" +storePracticeName)
sourceSheet.copyTo(newSpreadsheet, {contentsOnly: true})
newSpreadsheet.getSheetByName("sheet1").activate()
newSpreadsheet.deleteActiveSheet()
var pdfURLtemp = DriveApp.getFileById(newSpreadsheet.getId()).getAs("application/pdf")
var pdf = DriveApp.createFile(pdfURLtemp)
var pdfURL = pdf.getUrl()
Logger.log(pdfURL)
return pdfURL
}
function createQuery()
{
//Duplication check
var pdfCreated = "pdfCreated";
var pdfEmailed = "pdfEmailed";
var sheet = ss.getSheetByName("practicePivot");
var startRow = 2;
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var dataRange = sheet.getRange(startRow, 1, lastRow, lastColumn) ;
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i)
{
var row = data[i];
var practiceName = row[0]
var pdfCheck = row[2]
var copySelection = sheet.getRange(startRow + i, 1)
var copyData = copySelection.getValues()
var copyLocation = ss.getSheetByName("querySheet")
var copyCell = copyLocation.getRange("A2")
if (pdfCheck != pdfCreated)
{
var pdfURL = createPDF()
copyCell.copyTo(copyData)
sheet.getRange(startRow + i, 4).setValue(pdfURL)
sheet.getRange(startRow + i, 3).setValue(pdfCreated)
SpreadsheetApp.flush()
}
}
}
Your start row is 2:
var startRow = 2;
Your loop is looping until the count of the last row. So if there were 10 rows, and the data starts in row 2, then you need the loop to run 9 times. But your loop is running 10 times.
So you need to adjust the stop for the loop.
var L = data.length - 1;//The number of rows in the data - not the sheet
for (var i = 0; i < L; ++i)

Console.log gives correct output but when accessing set value it gives wrong output

Called Function:
this.findVerticalPossibleScoring = function(){
var possibilitySet = [];
for (var j = 0; j < 9;j++ ) {
for (var i = 0; i < 7; ){
var tempTile = this._tiles[i][j];
if(this.gameTilesValue[i][j]!=-1){
var tileTagValue = this.gameTilesValue[i][j];
if(this.gameTilesValue[i+1][j]==tileTagValue && this.gameTilesValue[i+2][j]==tileTagValue){
setElement = [];
do{
var tempPoint = this.makeArray(i,j);
setElement.push(tempPoint);
console.log(" verical i:"+i+" j:"+j);
i=i+1;
}while(i<9&&this.gameTilesValue[i][j]==tileTagValue);
possibilitySet.push(setElement);
continue;
}
}
i = i+1;
}
}
return possibilitySet;
};
this.makeArray = function (a,b){
console.log("element i:"+a+" j:"+b);
var arrayTemp = [];
arrayTemp.push(a);
arrayTemp.push(b);
return arrayTemp;
};
Calling function part:
if(scoringPossible == true){
//blast the tiles and add new tiles;
var verticalPossibleScoring = this.findVerticalPossibleScoring();
toBeDeletedTiles = [];
for(var i=0;i<verticalPossibleScoring.length;i++){
var tempSet = verticalPossibleScoring[i];
for(var j = 0;j<tempSet.length;j++){
var tempSetEntry = tempSet[i];
console.log("TILE i:"+tempSetEntry[0]+" j:"+tempSetEntry[1]);
}
}
}
I have added called function as well as calling function if loop as calling function is too big. I know this is infamous javascript loop issue. I am using gc-devkit game engine which is new and I new to it. I had solved the same issue for UIImage in it by creating custom class, but here I don't require custom array for it. Can any one guide me through this issue. Thanks in advance.
You use j as your loop variable when iterating over tempSet but then use i when getting elements from tempSet. Maybe just change
var tempSetEntry = tempSet[i];
to
var tempSetEntry = tempSet[j];

For Loop in object not functioning

I have leaflet object _test which looks like this
There are 4050 elements, and for all those elements I tried to run a loop and place label
var a = Object.keys(_test);
console.log(a.length);
j = 0;
for (var i = 0; i < a.length - 1; i++) {
var b = _test[a[i]];
var vdc = L.polygon(b._latlngs);
vdc_name = b.feature.properties.NAME_4;
var labelLocation = new L.LatLng(vdc.getBounds().getCenter().lat, vdc.getBounds().getCenter().lng);
var labelTitle = new L.LabelOverlays(labelLocation, vdc_name);
VDC_labels.addLayer(labelTitle);
console.log(vdc_name, j);
j++}
The output in console for console.log(a.length); is 4050. But the last output of
console.log(vdc_name, j);
is Sidin 1841, which means the loop runs only 1841 times. Can anyone please help me find out what i am doing wrong?
I also tried with this but the result is the same
for (ath in _test) {
var b = _test[ath];
var vdc = L.polygon(b._latlngs);
// console.log(i);
// i++
vdc_name = b.feature.properties.NAME_4; //label content
var labelLocation = new L.LatLng(vdc.getBounds().getCenter().lat, vdc.getBounds().getCenter().lng);
var labelTitle = new L.LabelOverlays(labelLocation, vdc_name);
VDC_labels.addLayer(labelTitle);
}
Solved.
Actually the problem is with the data i.e. in _test object the 1842nd element is a multipolygon unlike all other elements (polygon) so during the access of coordinate in
var b = _test[a[i]];
var vdc = L.polygon(b._latlngs);
b doesnot have the property _latlngs so the loop breaks..

Categories