Can someone help me how can I get the text between ; characters?
14;original;6711039;2;original;65535;9;6711039;52377;34;original;original
I need this:
var tag1:Number = 14
var tag2:String = original
var tag3:String = 6711039
var tag4:Number = 2
var tag5:String = original
... etc
how can I get it?
You are looking for String.split(), which takes a String and splits it into an array at a specific character.
var s:String = "14;original;6711039;2;original;65535;9;6711039;52377;34;original;original";
var a:Array = s.split(";");
var tag1:Number = a[0];
var tag2:String = a[1];
var tag3:String = a[2];
var tag4:Number = a[3];
var tag5:String = a[4];
Note: I didn't check if the tags are correctly aligned, so you may need to use different indexes.
Related
I am trying to merge different raster files into one using GEE. Since the data source of some of them is int, i am first transforming the rasters to float and then i am trying to merge them. However, the the output is missing some of the cells that i am trying to merge. The code I am using is:
var cell10 = cell_10.float()
var cell21 = cell_21.float()
var cell11 = cell_21.float()
var cell22 = cell_22.float()
var cell23 = cell_23.float()
var cell35 = cell_35.float()
var cell37 = cell_37.float()
var cell38 = cell_38.float()
var cell39 = cell_39.float()
var cell47 = cell_47.float()
var cell50 = cell_50.float()
var cell51 = cell_51.float()
var cell60 = cell_60.float()
var cell61 = cell_61.float()
var cell62 = cell_62.float()
var cell73 = cell_73.float()
var cell74 = cell_74.float()
var cell7 = cell_7.float()
var cell8 = cell_8.float()
var cell9 = cell_9.float()
var merged = ee.ImageCollection([cell10, cell21,cell11, cell22, cell23, cell35, cell37,
cell38, cell39, cell47, cell50, cell51, cell60, cell61, cell62, cell73, cell74, cell7, cell8, cell9,
worldcover]).mosaic()
I already tried to inspect each of the cells one by one by adding them to the map and printing them, and they all the same bands/ properties.
What am I doing wrong?
Thanks!
I'm attempting to combine two strings of rich text, while keeping their formatting.
function formatCombi() {
// get the sheet and the page
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// get the plaintext value of both cells, and their combined value
var text1 = sheet.getRange(1,1).getValue();
var text2 = sheet.getRange(1,2).getValue();
var text3 = text1 + text2;
var range = sheet.getRange(1,3);
// get the rich text value of both cells
var rtf1 = sheet.getRange(1,1).getRichTextValue();
var rtf2 = sheet.getRange(1,2).getRichTextValue();
// get the length of both strings and the combined length
var length1 = text1.length;
var length2 = text2.length;
var length3 = length1 + length2;
//get the formats of both strings
var bold1 = rtf1.getTextStyle().isBold();
var bold2 = rtf2.getTextStyle().isBold();
var italic1 = rtf1.getTextStyle().isItalic();
var italic2 = rtf2.getTextStyle().isItalic();
var under1 = rtf1.getTextStyle().isUnderline();
var under2 = rtf2.getTextStyle().isUnderline();
var strike1 = rtf1.getTextStyle().isStrikethrough();
var strike2 = rtf2.getTextStyle().isStrikethrough();
var colour1 = rtf1.getTextStyle().getForegroundColor();
var colour2 = rtf2.getTextStyle().getForegroundColor();
//create style for each string
const style1 = SpreadsheetApp.newTextStyle()
.setUnderline(under1)
.setBold(bold1)
.setItalic(italic1)
.setForegroundColor(colour1)
.build();
const style2 = SpreadsheetApp.newTextStyle()
.setUnderline(under2)
.setBold(bold2)
.setItalic(italic2)
.setForegroundColor(colour2)
.build();
//set the style for string based on the lengths
var rtf3 = SpreadsheetApp.newRichTextValue()
.setText(text3)
.setTextStyle(0,length1,style1)
.setTextStyle(length1,length3,style2)
.build();
range.setRichTextValues(rtf3);
}
This is the code I have so far, but for some reason it always spits out the same error code:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Range.setRichTextValues.
formatCombi # Code.gs:70
Does anyone know why this is? I cant see the point of RichTextBuilder being able to have varied formatting if I can't set it as a value, and the examples of this from the documentation work fine.
The API has numbers to define each of the data containers, I've tried to use several ways to be able to define these numbers, but without success.
In the image you can see the map clubes.262 clubes.264 clubes.265 clubes.266 clubes.275
My attempts:
var idclub = clubes[i].id;
var idclub = clubes.[i].id;
var idclub = clubes[0][i].id;
var idclub = clubes. + i + .id;
The complete script for easy viewing:
function MenuMercadoCartola1() {
var url = 'https://api.cartolafc.globo.com/atletas/mercado';
var response = UrlFetchApp.fetch(url);
var results = JSON.parse(response.getContentText());
var clubes = results.clubes;
var table = [['ID do Clubes','Nome do Clube']];
for (var i = 0; i < clubes.length; i++) {
var idclub = clubes[i].id;
var nameclub = clubes[i].nome;
table.push([idclub,nameclub]);
}
var sheet = SpreadsheetApp.getActive().getSheetByName('Menu');
sheet.getRange(1,1, table.length, table[0].length).setValues(table);
}
It appears that you should be using a 'for in' loop.
Your code revised:
for (var idclub in clubes)
{
idclub = +idclub;
var nameclub = clubes[idclub].nome;
table.push([idclub, nameclub]);
}
Your loop assumes clubes is an array however it is an object which means you could have to iterate on it using a for (var ... in ...) loop.
I can't seem to get the "g modifier" in a RegExp to work in a Google script.
When I try to apply it, sometimes I get the error that "ReferenceError: "g" is not defined.". When I remove the /g both regExp.exec and input.match(regExp) work, but only for the first match. Other times, I'll get the /g to work, but it returns null, not even producing the first match. I had attempted a while loop, but I didn't want to slow down this process even more (I'll save optimizing this script for another post once I get it to work as intended).
The short version is, I'm trying to get ALL matches (email addresses) not just the first one. Where do I apply the /g? Should I use another method?
You can see (below) what I've been attempting below.
Any tips? I appreciate any help me understand this and anyone that can show me a better way to approach this. Thanks!
if (colA != "" && colE != processed) {
var html = UrlFetchApp.fetch(colA).getContentText();
//Logger.log(html);
var regExp = new RegExp("[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}");
var regExp2 = new RegExp("[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}");
var regExp3 = '[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]/g {2,4}';
var regExp4 = '[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}/g';
var extractTest = html.match(regExp3);
//var extract = regExp.exec(html);
Logger.log(extractTest);
}
You can see the "bigger picture" of it all below.
//TEST
var processed = "YES";
function test() {
var ss = SpreadsheetApp.openById('1E4yUVIpwi00DzjfnXrZSgNFmVjOQbNWewxAiTBHRdD4');
var sheet = ss.getSheetByName('Sheet5');
var currentRow = 2;
var currentColumn = 1;
var numRows = sheet.getLastRow()-1;
var numColumns = 5;
var range = sheet.getRange(currentRow, currentColumn, numRows, numColumns);
var values = range.getValues();
//Logger.log(values);
for (var i = 0; i < numRows; ++i) {
var column = values[i];
var colA = column[0];
var colB = column[1];
var colC = column[2];
var colD = column[3];
var colE = column[4];
if (colA != "" && colE != processed) {
var html = UrlFetchApp.fetch(colA).getContentText();
//Logger.log(html);
var regExp = new RegExp("[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}");
var regExp2 = new RegExp("[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}");
var regExp3 = '[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]/g {2,4}';
var regExp4 = '[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}/g';
var extractTest = html.match(regExp3);
//var extract = regExp.exec(html);
Logger.log(extractTest);
}
}
//var destRange = sheet.getRange(currentRow+i,2);
//destRange.setValue(extract);
//var destRange2 = sheet.getRange(currentRow+i,5);
//destRange2.setValue(processed);
SpreadsheetApp.flush();
}
Using the solution provided by #I'-'I (see comments above): var re = /[A-z0-9._%+-]+#[A-z0-9.-]+\.[A-z]{2,4}/g;
How do I use appendText() or insertText() for a Google Doc script and maintain formatting?
I want to format the middle portion (group2) of appended strings with italics, while leaving the other parts (group1, group3) as normal text. For example: Hi my name is Nate.
I can bring in "Hi" and append "my name is" and it formats correctly. When I try to append (or insert) "Nate," "Nate" is italicized as well.Between operators +, appendText(), and insertText(), I'm not having much luck.
Below is the relevant portion of the script. Below that, is the entire thing.
How can I append 3 strings together, and only format the middle portion?
NOTE: I commented-out the things I tried (trial1, trial2, etc.). I also started HERE and used it as a guide.
Thanks for any help you can offer!
RELEVANT PART:
if (author1 != "") {
var group1 = author1+author2+author3;
var group2 = title2Italics+containerItalics;
var group3 = contribution1+contribution2+contribution3+version+number+publisher+pubDate+location;
//Only using the calculations below to determine the offset for insertText
var group1Length = group1.length;
var group2Length = group2.length;
var offset = group1Length+group2Length
Logger.log(group1Length);
Logger.log(group2Length);
Logger.log(offset);
//Determines if italicizing is necessary
if (group2.length > 0) {
var addG1 = body.insertParagraph(0,group1)
var addG2 = addG1.appendText(group2);
var formatItalics = addG2.editAsText().setItalic(true);
//var trial1 = addG2.editAsText().setItalic(true) + group3; //does not return the contents of "group3"
//var trial2 = formatItalics + group3; //does not return the contents of "group3"
//var trial3 = formatItalics.insertText(offset,group3); //Error: "Index (18) must be less than or equal to the content length (6)."
//var trial4 = formatItalics.insertText(group2Length, group3); //formats "group3" as well
//var trial5 = formatItalics.appendText(group3); //formats "group3" as well
}
//If italicizing is NOT necessary
else {
var cite = body.insertParagraph(0,group1 + group3);
} //ELSE STATEMENT ENDS HERE
} //FIRST IF STATEMENT ENDS HERE
ENTIRE SCRIPT:
function mlaBibTest() {
// Sheet Information
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.setActiveSheet(ss.getSheetByName('test'));
var startRow = 3;
var startCol = 21;
var numRows = sheet.getLastRow()-1;
var numCols = 14;
var dataRange = sheet.getRange(startRow, startCol, numRows, numCols);
// Document information
var doc = DocumentApp.openById('13MlHq_uoO1rUF0RfdF_kBlLJjbGt4aDoOcSWef0V4zM');
var body = doc.getBody();
// Fetch values for each row in the SS Range.
var cells = dataRange.getValues();
for (var i = 0; i < cells.length; ++i) {
var column = cells[i];
var colU = column[0];
var colV = column[1];
var colW = column[2];
var colX = column[3];
var colY = column[4];
var colZ = column[5];
var colAA = column[6];
var colAB = column[7];
var colAC = column[8];
var colAD = column[9];
var colAE = column[10];
var colAF = column[11];
var colAG = column[12];
var colAH = column[13];
var author1 = colU;
var author2 = colV;
var author3 = colW;
var title1Quotes = colX;
var title2Italics = colY;
var containerItalics = colZ;
var contribution1 = colAA;
var contribution2 = colAB;
var contribution3 = colAC;
var version = colAD;
var number = colAE;
var publisher = colAF;
var pubDate = colAG;
var location = colAH;
if (author1 != "") {
var group1 = author1+author2+author3;
var group2 = title2Italics+containerItalics;
var group3 = contribution1+contribution2+contribution3+version+number+publisher+pubDate+location;
//Only using the calculations below to determine the offset for insertText
var group1Length = group1.length;
var group2Length = group2.length;
var offset = group1Length+group2Length
Logger.log(group1Length);
Logger.log(group2Length);
Logger.log(offset);
//Determines if italicizing is necessary
if (group2.length > 0) {
var addG1 = body.insertParagraph(0,group1)
var addG2 = addG1.appendText(group2);
var formatItalics = addG2.editAsText().setItalic(true);
//var trial1 = addG2.editAsText().setItalic(true) + group3; //does not return the contents of "group3"
//var trial2 = formatItalics + group3; //does not return the contents of "group3"
//var trial3 = formatItalics.insertText(offset,group3); //Error: "Index (18) must be less than or equal to the content length (6)."
//var trial4 = formatItalics.insertText(group2Length, group3); //formats "group3" as well
//var trial5 = formatItalics.appendText(group3); //formats "group3" as well
}
//If italicizing is NOT necessary
else {
var cite = body.insertParagraph(0,group1 + group3);
} //ELSE STATEMENT ENDS HERE
} //FIRST IF STATEMENT ENDS HERE
} //FOR LOOP ENDS HERE
SpreadsheetApp.flush();
} // FUNCTION ENDS HERE
This is a simple example of doing what you asked. It's important to remember that setItalics(true) sets a persistent setting for all new text to be italic, so we have to set it back to false after.
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var paragraph = body.insertParagraph(0, ""); //add paragparh at top of body.
var text1 = paragraph.appendText("Not Italics ");
var text2 = paragraph.appendText("Italics ");
text2.setItalic(true); //Everything after and including this will be italics
var text3 = paragraph.appendText("Not Italics");
text3.setItalic(false); //Everything after and including this will not be italics
>Not Italics Italics Not Italics
So it's easier if you set italics as you build the paragraph.