I need to change the color of some word for each row Table
I have a table for wordInput that's mean contains the word that I need to color them and the second Table contain Titles that I need to color some words for it.
My script work when the number of row title equal to the number of
the row wordInput and That's wrong! because sometimes I have only 1
row as an input. I need to find solution to how can do the loop for
each row Title I got errors evaluating script when I need to
change the color of the second-row Title
TypeError: Cannot call method "split" of null (#4)
this error because now we ARE in the second Row InputWords and it NULL without Value
var row = {
"Title": ["This reference is to serve as a useful","Title2","Title3"],"wordInput":["serve"] }
//Title contains 3 rows : as string Values
//wordInput contains words that i need to color them in Title :As a string Value
var words = row["wordInput"];// this contain only 1 row ["serve"]
var orgTitle = row["Title"];
words.split(" ").forEach(function (word) {
orgTitle = orgTitle.replace(new RegExp(word, "g"),'<span style="color: red;">'+word+'</span>');
});
orgTitle;
I'm looking for your help thanks
Assuming row.title is the array that contains all the words that should be highlighted, this is what you need.
var row = {
"Abstract": "This reference is to serve as a useful reference for testing whether the styling of reference, works or not. All occurances of 'reference' should be given a color of red.",
"Title":["reference" ,"testing"]
};
var wordsToHighlight = row['Title'];
var result = row["Abstract"];
wordsToHighlight.forEach(function (word) {
result = result.replace(new RegExp(word, "g"),'<span style="color: red;">'+word+'</span>');
});
document.querySelector("#result").innerHTML = result;
<div id="result">
</div>
Related
I have a html textarea element in my page where user gives comma separate values. for example below.
A-48402,AA,SBAFL,AA+,USD,,
From javascript (which I prefer) I am applying logic to check if the last row value is blank (separated by comma only) then to put a String value 'Y'. Thus I am writing the below
var data = document.getElementById('txid').value;
rows = data.split('\n');var row1 = rows[0];row1Values=row1 .split(',');
Then I am applying logic to verify whether the last value for every row is blank or not, which is actually blank, then adding the below.
row_values.push('Y');
It is reflecting in debugger.
But what I see is the value 'Y' in the Java action class is not reflecting and showing usual 'Y' while the page submit. How can I add this value 'Y' in every rows end (where there is blank) so that it will be visible in action class?
String Data = request.getParameter('mbs_inst_data');
This data is populated with the same blank values.
If you're only checking for the last row then the only case that would happen is when it's ,,
so you can just do a simple check
let data = 'A-48402,AA,SBAFL,AA+,USD,,'
data = data.split(',')
let lastRowIsBlank = data[data.length-2] === ""
// we are doing length - 2 because in your situation we have 2 ""
// since you have two commas. If you have only 1 comma we would
// the same steps but with length - 1
if(lastRowIsBlank) data[data.length-2] = "Y"
return data.toString()
You can use it like this.
<p id="demo">Visit Microsoft! ,,</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace(/,,$/, ',Y');
document.getElementById("demo").innerHTML = res;
}
</script>
Output
Visit Microsoft! ,Y
I guess this would help you.
I'm trying to post an array into column P daily. Because it's posting daily, I'd like it to also grab the last row.
I'm pulling from another workbook but don't know how to get the paste to work.
Function () {
var toCopy = [copies data from spreadsheet 1]
/* trying to paste into Aggregate tab in spreadsheet 2*/
var ss2=SpreadsheetApp.openById(‘spreadsheet 2’);
var tsh2=ss2.getSheetByName('Aggregate');
var tsh2.getLastRow(???
}
How do I paste var toCopy into column P? The array is 15 columns wide.
You want to append the values to the next row of the last row of column "P" in the sheet of "Aggregate".
The values are 2 dimensional array which has 600 rows and 15 columns.
The number of rows is changed every run.
The number of columns is not changed.
After the values are put, you want to retrieve the last row number.
I could understand like above. If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Flow:
Retrieve the last row number of the column "P".
From your question, I'm not sure whether the last row of the column "P" is the last row of the sheet. So the last row is checked for only column "P".
Append "toCopy" to the next row of the last row of column "P".
Retrieve the current last row number.
Modified script:
Please set the values of toCopy and the Spreadsheet ID.
function myFunction() {
var toCopy = [copies data from spreadsheet 1]
var ss = SpreadsheetApp.openById('###');
var sheet = ss.getSheetByName('Aggregate');
// Retrieve the last row of the column "P".
var lastRowOfColP = 0;
var valuesOfColP = sheet.getRange("P1:P").getValues();
for (var i = valuesOfColP.length - 1; i >= 0; i--) {
if (valuesOfColP[i][0]) {
lastRowOfColP = i + 1;
break;
}
}
// Put "toCopy" to the next row of the last row of column "P".
sheet.getRange(lastRowOfColP + 1, 16, toCopy.length, toCopy[0].length).setValues(toCopy);
// Retrieve the current last row number.
var currentLastRow = lastRowOfColP + toCopy.length;
Logger.log(currentLastRow)
}
This modified script supposes that toCopy is 2 dimensional array.
References:
setValues(values)
getRange(a1Notation)
getRange(row, column, numRows, numColumns)
Looks like Set a range's values is what you're after.
function () {
var values = [
// Copied data from spreadsheet 1
]
// Paste into Aggregate tab in spreadsheet 2
var ss2 = SpreadsheetApp.openById(‘spreadsheet 2’);
var sheet2 = ss2.getSheetByName('Aggregate');
var range = sheet2.getRange('P1:P15');
range.setValues(values);
// Grab last row
var lastRow = sheet2.getLastRow();
}
PS: Watch out for the formatting in your code. It looks like you might not be using a proper text editor. Your formatting made function be Function and the quotations 'spreadsheet 2' become ‘spreadsheet 2’. I use VS Code and Vim, for example.
I have this web app in Appscript that receives info from a POST request from Siri Shortcuts.
function doPost(e) {
//Get active spreadsheet and input database tab name.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Test02');
if(typeof e !== 'undefined')
//Retrieve data received and make a string.
var data = JSON.stringify(e);
//1st print. Print string on new row.
sheet.getRange(sheet.getLastRow()+1,1).setValue(data);
//Eliminate all special characters coming from Siri Shortcuts POST request.
data = data.replace(new RegExp(["\\\\"], 'g'), "");
data = data.replace(new RegExp('"', 'g'), "");
data = data.replace(new RegExp("{", 'g'), "");
data = data.replace(new RegExp("}", 'g'), "");
data = data.replace(new RegExp(":", 'g'), "");
//Split all info on the left side of the first parameter and on the right side of the second.
data = data.split('Log')[1];
data = data.split("name")[0];
//2nd print. Print clean string on a new row.
sheet.getRange(sheet.getLastRow()+1,1).setValue(data);
//3rd print. Print all array element on row. End goal is to have them start from column A and keep adding 1 column every loop.
//Split string into array.
var dataArray = data.split(",");
var lastRow = sheet.getLastRow()+1;
for(var i = 0; i < dataArray.length; i++) {
//3rd print, first part. This tests the real data.
sheet.getRange(lastRow,1+[i]).setValue(dataArray[i]);
//3rd print, second part. This tests the index number to try and find why row are being skipped.
sheet.getRange(lastRow+1,1+[i]).setValue([i]);
}
//Use Mail app to send mail after completion.
MailApp.sendEmail("abc123#gmail.com","InningLogger - Test", data);
return;
}
Attached is the result I am getting in the spreadsheet.
I have two problems with rows 3 and 7:
Why is the data not starting from column A?
Why is row 7 skipping a ton
of columns (I hid them for the screenshot) to print the last 3
values?
Thanks!
As #Pointy mentioned, the problem lied within the 1+[i] in the for loop. "Because i is a number in your code, say 3, 1+[i] will be the string "13", not a number. Changing this to [i+1] solved the problem completely.
We have a front end form where users enter the below feed. I would need to validate the text, display a popup if there is a word "Y" instead of "X" after the first ":" and clear the row which has Y in it.
20170731_5412_1234:X:Hello:P:James::999999999
20170731_5412_5678:X:Gopal::Varma::999999999
20170731_5412_1234:X:Steve:H:Benton::999999999
20170731_5412_2321:Y:Varme::Dost::999999999
For example:
In the above feed the 4th line has "Y" after the first column. I need to display a popup stating the user Varme has Y and it will be cleared. The validation should be performed on multiple rows.
Try something like this, where you split your text result first on the newline character ("\n") and then by colons:
var input,
cwidRows,
results,
i;
// divide input string into an array of strings representing each line
input = stringFromTextArea;
cwidRows = input.split("\n");
// put acceptable results into results array for later use
results = [];
for (i = 0; i < cwidRows; i++) {
if (cwidRows[i].split(":")[1] !== "Y") results.push(cwidRows[i]);
}
I have numerous tsv files each with header row. Now one column name in header row is age. In few files, column name is age while in other files it has EOL charcter such as \r \n.
Now how can i use str.indexOf('age') function so that i get index of age irrespective of column name age with EOL character such as \n , \r etc..
Foe eg:
tsv file1:
Name Address Age Ph_Number
file 2:
Name Address Age/r
file 3:
Name Address Age\n
I am trying to find index of age column in each files header row.
However when i do-
header.indexOf('age')
it gives me result only in case of file1 because in other 2 files we have age as age\r and age\n..
My question is how should i find index of age irrespective of \r \n character along with age in header row.
i have following script now:
var headers = rows[0].split('\t');
if (file.name === 'subjects.tsv'){
for (var i = 0; i < rows.length; i++) {
var ageIdColumn = headers.indexOf("age");
console.log(headers)
As I stated in the comments, indexOf() returns the starting position of the string. It doesn't matter what comes after it:
var csvFile1 = 'column1,column2,column3,age,c1r1';
var csvFile2 = 'column1,column2,column3,age\r,c1r1';
var csvFile3 = 'column1,column2,column3,age\n,c1r1';
console.log(csvFile1.indexOf("age"));
console.log(csvFile2.indexOf("age"));
console.log(csvFile3.indexOf("age"));
If you specifically want to find the versions with the special characters, just look for them explicitly:
var csvFile4 = 'column1,age\r,column2,column3,age\n,c1r1';
console.log(csvFile4.indexOf("age\r"));
console.log(csvFile4.indexOf("age\n"));
Lastly, it may be that you are confused as to what, exactly indexOf() is supposed to do. It is not supposed to tell you where all occurrences of a given string are. It stops looking after the first match. To get all the locations, you'd need a loop similar to this:
var csvFile5 = 'column1,age\r,column2,age, column3,age\n,c1r1';
var results = []; // Found indexes will be stored here.
var pos = null; // Stores the last index position where "age" was found
while (pos !== -1){
// store the index where "age" is found
// If pos is not null, then we've already found age earlier and we
// need to start looking for the next occurence 3 characters after
// where we found it last time. If pos is null, we haven't found it
// yet and need to start from the beginning.
pos = csvFile5.indexOf("age", pos != null ? pos + 3 : pos );
pos !== -1 ? results.push(pos) : "";
}
// All the positions where "age" was in the string (irrespective of what follows it)
// are recorded in the array:
console.log(results);