Looping through Spreadsheet to collect data starting with xxx_bb - javascript

I am confused about the following task.
Eventually, I want to find certain data in a spreadsheet.
This is not a problem, I can find a defined value and grab whatever information I need around it through:
function findCell() {
var ss = SpreadsheetApp.openById('ID');
var sheet = ss.getSheetByName('NAME');
var dataRange = sheet.getDataRange();
var values = dataRange.getValues();
for (var i = 0; i < values.length; i++) {
var row = "";
for (var j = 0; j < values[i].length; j++) {
if (values[i][j] == "Hallo") {
row = values[i][j+11];
Logger.log(row);
Logger.log(i);
}
}
}
}
But I am not trying to find the exact value "Hallo" but anything starting with "Hallo_xxx". I tried to use i.e. indexOf but I am not sure what I am doing wrong - this task doesn't sound too difficult to solve. Does anybody got a good idea here...?
Thanks for your advice,
Sa

Here's some code I just wrote and tested. findCell() should do what you want. As you surmised, indexOf() will quickly find specific text within a string, but I don't see where you tried to use it in your code.
var ROW = 0;
var COLUMN = 1;
function test(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var results = findCell("Hallo_xxx", sheet);
for(var i=0 ; i<results.length ; i++){
Logger.log("Match found at: " + colToA1(results[i][COLUMN]) + (results[i][ROW] + 1));
}
}
function findCell(string, sheet) {
var range = sheet.getDataRange();
var values = range.getValues();
var results = [];
for(var i=0 ; i<values.length ; i++){
for(var j=0 ; j<values[0].length ; j++){
if(values[i][j].indexOf(string) != -1){
results.push([i, j]);
}
}
}
return results;
}
function colToA1(col){
return col < 26 ?
String.fromCharCode(col + 65) :
String.fromCharCode(parseInt(col / 26) + 64) + String.fromCharCode((col % 26) + 65);
}

Working on this a bit, the answer I was looking for is:
function findCell2() {
var ss = SpreadsheetApp.openById('ID');
var sheet = ss.getSheetByName('Name');
var values = sheet.getDataRange().getValues();
for(var i=1;i<values.length;i++){
if((values[i][1].indexOf('Hallo') > -1){
Logger.log('xxx: ' + values[i][2]);
}
}
}

Related

Pulling data from an API using Google App Script

I'm hoping someone could help as I've spent the last day trying to figure out where I'm going wrong.. to no avail.
I'm trying to pull some basic information from https://nvd.nist.gov/ using their API (https://services.nvd.nist.gov/rest/json/cve/1.0/cve-id). I need to pull the data for cve-id's. cve-id's are in column A.
Example cve-id: https://services.nvd.nist.gov/rest/json/cve/1.0/CVE-2019-9763
The only information I want is the description, cvssV3baseScore, cvss3vectorString. Very basic you might think but I'm lost after doing tons of research.
I've tried doing this using the following code in Google Apps Script:
function fetchData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("CVEs");
var sheetData = sheet.getDataRange().getValues();
var i, len = sheetData.length, row = [];
for (i = 1; i < len; i++) {
if (sheetData[i][0] == "" || sheetData[i][15] != "")
continue;
// sheetData[i][8] - here 8 represents column I as column A = 0. Column 23 = x, this is where sheet ends (at new Date())
let url = 'https://services.nvd.nist.gov/rest/json/cve/1.0/' + sheetData[i][0];
try {
var id = json.result.CVE_items.cve.CVE_data_meta.ID;
var idStr = '';
var response = UrlFetchApp.fetch(url).getContentText();
row.push([
idStr,
json.result.CVE_items.cve.CVE_data_meta.ID,
json.result.CVE_items.cve.description.description_data.value,
json.result.CVE_Items.impact.baseMetricV3.cvssV3.vectorString,
json.result.CVE_Items.impact.baseMetricV3.cvssV3.baseScore,
]);
//Here (middle number) 10 number denotes the exact column number from where we need to write data. 10 = J
sheet.getRange(i + 1, 2, 1, row[0].length).setValues(row);
}
catch (e) {
continue;
}
}
}
function lookupByNthValue(search_key, sourceColumn, targetColumn, n) {
if(arguments.length < 4){
throw new Error( "Only " + arguments.length + " arguments provided. Requires 4." );
}
var count = 1;
for(var i = 0; i < sourceColumn.length; i++){
if(sourceColumn[i] != search_key){
continue;
}
if(count == n){
return targetColumn[i];
}
count++;
}
}
function onOpen() {
SpreadsheetApp.getUi()
.createMenu("Actions")
.addItem("Fetch Data", 'fetchData')
.addToUi();
}
I would much appreciate if someone could help, link to the Google sheet: https://docs.google.com/spreadsheets/d/18ilCE1udUM87c4YtxXAB52Fm3mEzDk8UggcH96S3JLA/edit?usp=sharing
Thank you in advance.
There are a few problems: len equals 1000 because you have data on row 1000! therefore the script will be too long. You write CVE_items instead of CVE_Items with a capital letter! CVE_Items is not a parent, it is an array, even with a single occurrence. You never parse the response of url fetch. Try this
function fetchData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("CVEs");
var sheetData = sheet.getDataRange().getValues();
var i, len = sheet.getLastRow(), row = [];
Logger.log(len)
for (i = 1; i < len; i++) {
try{
let url = 'https://services.nvd.nist.gov/rest/json/cve/1.0/' + sheetData[i][0];
var json = JSON.parse(UrlFetchApp.fetch(url).getContentText())
var row=[]
row.push([
json.result.CVE_Items[0].cve.CVE_data_meta.ID,
json.result.CVE_Items[0].cve.CVE_data_meta.ID,
json.result.CVE_Items[0].cve.description.description_data.value,
json.result.CVE_Items[0].impact.baseMetricV3.cvssV3.vectorString,
json.result.CVE_Items[0].impact.baseMetricV3.cvssV3.baseScore,
]);
Logger.log(row)
sheet.getRange(i + 1, 2, 1, row[0].length).setValues(row);
}catch(e){}
}
}

Why does this for loop not work in Google scripts

i wrote the following loop based on the code found here:
How do I iterate through table rows and cells in javascript?
function myRowLooper() {
var inputSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('INPUT');
var inputRange = inputSheet.getRange(2,inputSheet.getLastColumn(),inputSheet.getLastRow());
for (var i = 0, row; row = inputRange.rows[i]; i++) {
Logger.log(row);
for (var j = 0, col; col = row.cells[j]; j++) {
Logger.log(col);
}
}
}
but when I apply it to Google scripts it throws an error: "TypeError: Cannot read property "0" from undefined."
What's causing this?
Because you can't get any value from 'inputRange.rows[i]'.
You may do something like this :
function myRowLooper() {
var inputSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
for(var i = 1; i<=inputSheet.getLastRow(); i++){
for(var j = 1; j<=inputSheet.getLastColumn(); j++){
var cell = inputSheet.getRange(i,j).getValue();
Logger.log(cell);
}
}
}
Hope this will help you.
Thanks.
Your code is extremely sloppy. You are trying to combine variables and condense unnecessarily and it's leading to errors in your code. There's no use in added "efficiency" if it leads to errors and mistakes.
Try something like this --
function yourFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Name");
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getMaxColumns();
var conditionToCheckColumn = (lastColumn - 1);
var conditionRange = sheet.getRange(2, checkColumn, (lastRow - 1), 1);
var check = checkRange.getValues();
for (var i = 0; i < check.length; i++) {
if (check[i] == condition) {
continue;
} else {
//your code here
}
}
}
This will pull a range at which you can check it's value/ condition and if matches, it does nothing. If it does not match, it will perform code. It will then loop to the next row until the last row in your check range that has data.
Be warned - functions count as data despite the cell being visibly empty. If your sheet uses functions like =QUERY, you will have an infinitely looping code unless your =QUERY (or other fx()) has a specific upper limit.

How to Setvalues off of an array google GAS for spreadsheets

I have been using google scripts for a week, and I have searched as much as I could to get the answer. Can someone please help me? I wrote a simple script to evaluate if a course is online based on the last three digits of a course number(i.e PSY-250-400). The script works fine, and I pushed the result into the end of the array. I don't know how to write back to google sheets. Below is what I have. Currently it will set the values based on the first result(online course). So all values are set to online. I am running it on 7 rows right now, but will need to run it on 20,000.
function onlineonly(online){
var sheet = SpreadsheetApp.getActiveSheet();
var students = sheet.getRange('A2:D7').getValues();
var online = ["400","401","403","404","600"];
var m;
var section;
for(var i=0; i<students.length; ++i){
section = students[i][3].substring(8,13);
for(var j = 0;j<online.length; j++){
if(section===online[j]){
section = m;
}
}
if(section === m){
students[i].push("online");
} else {
students[i].push("not online");
}
var method = [];
for(var k = 0; k<students.length; k++){
if(students[i][4]=== "online"){
method = "online";
} else {
method = "in person";
}
sheet.getRange('c2:c7').setValue(method);
}
}
}
The important thing to remember is that the dimensions of the Range must equal the exact dimensions of the Array[][]. This array must be two-dimensional! Otherwise you'll get an error that setValues() method expects an Object[][], not an Array.
You're trying to set a simple array. Also, the method you'll use is setValues(), not setValue().
Your code is a little hard to understand, so this is an example of the pattern:
function writeOutValues() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("C2:C7");
var values = range.getValues();
//remember, a 2d array is a grid of values that corresponds to the grid of the range like so: values[row][column]
//so in this range, values[0][0] = C2, values[1][0] = C3, etc.
values[0][0] = "New Value";
values[1][0] = "Another one";
//to set value,
range.setValues(values);
}
just move inner for loop
for(var k = 0; k<students.length; k++) to outside the main for loop
and apply the technique as told by zbnrg
here is working code
function onlineonly()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var students = sheet.getRange('A2:C4').getValues();
var online = ["400","401","403","404","600"];
var m;
var section;
for(var i=0; i<students.length; ++i)
{
section = students[i][1].slice(8,11);
for(var j = 0;j<online.length; j++)
{
if(section===online[j])
{
section = m;
}
}
if(section === m)
{
students[i].push("online");
}
else
{
students[i].push("not online");
}
}
var range = sheet.getActiveSheet().getRange("C2:C4");
var method = range.getValues();
for(var k = 0; k<students.length; k++)
method[k][0] = students[k][3]==="online"?"online":"in person";
Logger.log(method[0][0] +" "+method[1][0] +" "+ method[2][0])
range.setValues(method);
}
here is my spreadsheet https://docs.google.com/spreadsheets/d/1iA9v_3rPH9JAhAmt2EJTdbqTQkFEl7yewawPy3w4YIg/edit#gid=0

Google Spreadsheet Script: "Cannot find function getRange in object Sheet" when creating a simple function

Sorry, for the stupid question, but I´ve searched the whole internet and I could not find a good Tutorial to learn how to program in Google SpreadSheet Script.
I want to make a very simple function just for practice.
function simplesum(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets();
var range = sheet.getRange(input);
var x = 0;
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
var cell = range.getCell(i, j);
x += (cell.getValue());
}
}
return x;
}
I know I could use =sum() to do exactly the same thing. The idea here is to learn how to program.
When I try to use my function in a cell: (i.e: =simplesum((A1:A8)) it gives an Error saying: "TypeError: Cannot find function getRange in object Sheet. (line 4)"
What should I do?
And again, sorry for the dumb question....
In this case, you are implementing a Google Apps Script function as a custom function, invoked in a spreadsheet cell.
When you pass a range to a custom function invoked in a spreadsheet cell, you are not passing a range object or a range reference, but rather a 2-D Javascript array of values. So your custom function should just process that array.
function simplesum(input)
{
var x = 0;
for (var i = 0; i < input.length; i++)
{
for (var j = 0; j < input[0].length; j++)
{
x += input[i][j];
}
}
return x;
}
This is working :
function sum(input) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); // your mistake : getSheets()
var range = sheet.getRange(input);
var x = 0;
for (var i = 1; i <= range.getNumRows(); i++) {
for (var j = 1; j <= range.getNumColumns(); j++) {
var cell = range.getCell(i, j);
x += cell.getValue();
}
}
return x;
}
function main () // Yes, I am a former C-programmer ...
{
var s = sum ("A1:B3"); // Notice the quotes. A string must me entered here.
Logger.log('s = ' + s);
}
var sheet = ss.getSheets();
returns an Array of sheets, meaning sheets.getRange(input) will throw that error. Try this instead:
var sheet = ss.getSheets()[0];
which selects the first sheet of the array of sheets. Google has some decent documentation for this. For example, here's its documentation on getRange(). Note that it uses ss.getSheets()[0] as well. Hope this helped!

JavaScript syntax issue

I'm doing "fifteen puzzle" game. I'm only a beginner, so I chose this project to implement. My problem is shuffle algorithm :
function shuffle() {
$('td').empty();
var p = 0;
var f = 0;
do {
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
var rand = arr[Math.floor(Math.random() * arr.length)];
if ($('#' + rand).is(':empty')) {
p = p + 1;
document.getElementById(rand).textContent = p
var f = $('td').not(":empty").length;
} else {}
} while (f < 15)
That works cool, but I've heard that almost 50% of all random shuffle like mine is unsolvable. So I found math formula at wikipedia.org for this game, explaining how you can avoid that.
Here's modified algorithm that doesn't work either. The way I know it is alert stuff: it launches only 2 times instead of 31.
array = [];
function algorithm (){
// alert('works')
for (var c=16; c<17; c++){
document.getElementById(c).textContent = '100';
}
for (var i=1; i<16; i++){
var curId = document.getElementById(i).id;
var curIdNum = Math.floor(curId);
alert('works')
var curIn = document.getElementById(i).textContent;
var curInNum = Math.floor(curIn);
array.push(i);
array[i] = new Array();
for (var j=1; j<15; j++){
var nextId = curIdNum + j; //curIdNum NOT cerIdNum
var nextIn = document.getElementById(nextId).textContent;
//alert('works')
if (nextId < 16){
var nextInNum = Math.floor(nextIn);
if (curInNum > nextInNum){
array[i].push(j)
}
}
}
var sum = 0;
for (var a=0; a<15; a++){
var add = array[a].length;
sum = sum + add;
}
var end = sum + 4;
if (end % 2 == 0){
document.getElementById('16').textContent = "";
}
else {
shuffle();
}
}
}
The question is the same:
What's wrong? Two-dimensional array doesn't work.If you've got any questions - ask.
Just to make it clear: 2 for loops with i and j should make a 2-dimensional array like this [ this is " var i" -->[1,3,4,5,7], this is "var i" too-->[5,7,9,14,15]]. Inside each i there's j. The for loop with var a should count the number of js inside each i. if the number of js is even, the code is finished and shuffle's accomplished, otherwise shuffle should be made once again.
var nextId = cerIdNum + j;
in that fiddle, I don't see this cerIdNum declared & defined neither as local nor as global variable, I suppose that is curIdNum
Please use the below definition of algorithm and let us know if this works. Basically, the alert messages would come only twice, since there were usages of undefined variables. For the purpose of illustration, I have placed comments at where the problem points occured. Due to these problems, your script would stop executing abruptly thereby resulting in the behavior you described.
Oh and by the way - I did not have time to go through the Wiki link provided - hence you will have to verify your logic is correct. However, I have definitely resolved the errors causing the behavior you observed.
As an aside - consider using jQuery, your code will be a lot cleaner...
function algorithm (){
// alert('works')
for (var c=16; c<17; c++){
document.getElementById(c).textContent = '100';
}
for (var i=1; i<16; i++){
var curId = document.getElementById(i).id;
var curIdNum = Math.floor(curId);
alert('works')
var curIn = document.getElementById(i).textContent;
var curInNum = Math.floor(curIn);
array.push(i);
for (var j=1; j<15; j++){
var nextId = curIdNum + j; //curIdNum NOT cerIdNum
var nextIn = document.getElementById(nextId).textContent;
//alert('works')
if (nextId < 16){
var nextInNum = Math.floor(nextIn);
if (curInNum > nextInNum){
array.push(j) //array[i].push does not make sense
}
}
}
var sum = 0;
for (var a=0; a<15; a++){
var add = array.length; //array[1].length does not make sense
sum = sum + add;
}
var end = sum + 4;
if (end % 2 == 0){
document.getElementById('16').textContent = "";
}
else {
shuffle();
}
}
}
I found the solution by totally rewriting the code. Thank everyone for help!
Here's what do work:
function shuffle (){
press = 1;
$('td').empty().removeClass();
p=0;
var f;
do {
var arr=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
var rand=arr[Math.floor(Math.random()*arr.length)];
if ($('#'+ rand).is(':empty')){
p = p + 1;
document.getElementById(rand).textContent = p
var f = $('td').not(":empty").length;
}
else{}
}while(f < 15){
winChance();
}
}
function winChance (){
array = [];
for (i=1;i<16;i++){
array[i]= new Array();
var currentId = $('#' + i).attr('id');
var currentIn = $('#' + i).html()
var currentIdNum = parseInt(currentId, 10);
var currentInNum = parseInt(currentIn, 10);
for (j=1;j<16;j++){
var nextId = currentIdNum + j;
if (nextId < 16){
var nextIn = $('#' + nextId).html();
var nextInNum = parseInt(nextIn, 10);
if (currentInNum > nextInNum){
array[i].push(j);
}
}
}
}
checkSum();
}
function checkSum(){
var sum = 0;
for (var a=1; a<16; a++){
var add = array[a].length;
sum = sum + add;
}
var end = sum + 4;
if (end % 2 == 0){}
else {
shuffle();
}
}

Categories

Resources