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){}
}
}
Related
I have the below script which essentially looks at a google sheets tab, which has some data that needs to be uploaded to Google Import API (Google Analytics):
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('UK Only');
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var rows = ss.getRange(1, 1, maxRows, maxColumns).getValues();
var rowsCSV = rows.join("\n")
var blobData = Utilities.newBlob(rowsCSV, "application/octet-stream", "GA import data");
try {
var upload = Analytics.Management.Uploads.uploadData("10131233", "UA-1234576-2", "righfghfghfgT8Dox1nwXDg", blobData);
Logger.log("Test Data Import Successful");
}
catch (e) {
console.error(e);
}
The output is something like the below:
ga:productSku,ga:productName,ga:productBrand,ga:productCategoryHierarchy,ga:dimension25,ga:dimension28,ga:dimension31
456456456,example value wit,h char ,that "breaks" csv,fgjfgjf Tjghjghjg,FP,Women,dasdasd
456456456,example value wit,h char ,that "breaks" csv,123123123,FP,Women,dasdasd
456456456,example value wit,h char ,that "breaks" csv,Rdasdasd,FP,asdasdasd
The above shows some values that will break the CSV format (commas and quotes).
How do I go about properly formatting the CSV so that it will not break on these characters?
Managed to resolve this with the below function, I had to account for quotes and commas within values that Google API doesn't like:
function arrayToCSV (twoDiArray) {
var csvRows = [];
for (var i = 0; i < twoDiArray.length; ++i) {
for (var j = 0; j < twoDiArray[i].length; ++j) {
twoDiArray[i][j] = '\"' + twoDiArray[i][j].replace('"','""') + '\"';
}
csvRows.push(twoDiArray[i].join(','));
}
return csvRows.join('\r\n');
};
Making sure to call it with the below:
var maxRows = ss.getLastRow();
var maxColumns = ss.getLastColumn();
var rows = ss.getRange(1, 1, maxRows, maxColumns).getValues();
var rowsCSV = arrayToCSV(rows);
I'm very new to Javascript but have recently started modifying scripts to let us pull various csv reports into Google Sheets, using Google Apps Script. The current script we have for this reads the csv file and then inserts all data after the 8th row (since the rows before that just contain info such as when it was generated, report name etc.).
The challenge now is that we need a year-to-date report that is split by month into Sheets (if we split by day we reach the 2 million cell limit). But as the reporting tool in this case doesn't allow to segment by any other date variable than day, we have to do the split within the script. So we would basically have a YTD report sent with ad-level data split by day. This ad-level data is then grouped by month rather than day within the script, before the data is inserted into Sheets. In other words, I want e.g. cost, impression and click data for an ad to be summed for all daily occurrences within one month.
I've looked around intensively the last couple of days but haven't found a working solution for this specific problem yet. I would be super grateful if someone wanted to take a look at this.
For your reference, please find two scripts below. The first one is the working script we currently have, which just insert the csv data from the 8th row (but no grouping):
function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet_name');
sheet_atlas.getRange('A2:V10000').clearContent();
var sheetName = "sheet_name"
var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];
for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();
var regExp = new RegExp('a href="(.*?)"', "gi");
//var regExp = new RegExp('data-saferedirecturl="(.*?)"', "gi"); // "i" is for case insensitive
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)
if (eachRow>8)
{
var theRow = []
theRow.push(csvdata[eachRow][0])
theRow.push(csvdata[eachRow][1])
theRow.push(csvdata[eachRow][2])
theRow.push(csvdata[eachRow][3])
theRow.push(csvdata[eachRow][4])
theRow.push(csvdata[eachRow][5])
theRow.push(csvdata[eachRow][6])
theRow.push(csvdata[eachRow][7])
theRow.push(csvdata[eachRow][8])
theRow.push(csvdata[eachRow][9])
theRow.push(csvdata[eachRow][10])
theRow.push(csvdata[eachRow][11])
theRow.push(csvdata[eachRow][12])
theRow.push(csvdata[eachRow][13])
theRow.push(csvdata[eachRow][14])
theRow.push(csvdata[eachRow][15])
theRow.push(csvdata[eachRow][16])
theRow.push(csvdata[eachRow][17])
theRow.push(csvdata[eachRow][18])
theRow.push(csvdata[eachRow][19])
theRow.push(csvdata[eachRow][20])
theRow.push(csvdata[eachRow][21])
newOrders.push(theRow)
}
}
}
}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,1, newOrders.length, newOrders[0].length).setValues(newOrders)
}
The second one is my current attempt at creating the grouping. So far I've managed to break out the month, but when it comes to the summing part it goes all wrong.
function importData() {
var sheet_atlas = SpreadsheetApp.getActive().getSheetByName('sheet name');
sheet_atlas.getRange('L2:AA60000').clearContent();
var sheetName = "sheet name"
var threads = GmailApp.search("attachment_name")
var msgs = GmailApp.getMessagesForThreads(threads);
var newData = [];
for (var i = 0 ; i < msgs.length; i++) {
for (var j = 0; j < msgs[i].length; j++) {
var attachments = msgs[i][j].getAttachments();
var bodyEmail = msgs[0][0].getBody();
var regExp = new RegExp('a href="(.*?)"', "gi");
var url = regExp.exec(bodyEmail)[1];
Logger.log(url)
var decode = new XML('<d>' + url + '</d>');
var strDecoded = decode.toString()
var response = UrlFetchApp.fetch(strDecoded).getContentText();
var csvdata = Utilities.parseCsv(response)
var newOrders = []
//Logger.log(csvdata)
var currMonth = "";
var theRow = [];
var columns = 11;
var formattedMonth = "";
for (var eachRow in csvdata){
//for(var col1 in csvdata[8]){
//if(csvdata[8][col1] == 'Campaign Name'){ var campaignCol = col1 }
//else if(csvdata[8][col1] == 'Publisher Name'){ var publisherCol = col1 }
//else if(csvdata[8][col1] == 'Statistics Date'){ var dateCol = col1 }
//}
//Logger.log(dateCol)
//Logger.log(publisherCol)
//Logger.log(campaignCol)
if (eachRow>8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}
formattedMonth = date[0] + "-" + date[2]
for(var cy = 0; cy < columns; cy++) {
if(cy != 2){
// Sum the columns
theRow[cy] = parseFloat(theRow[cy]) + parseFloat(csvdata[eachRow][cy])
}
}
}
}
// Pushing final row
Logger.log(formattedMonth)
newOrders.push(theRow)
}
Logger.log(newOrders)
SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName).getRange(2,12, newOrders.length, newOrders[0].length).setValues(newOrders)
}
}
Thanks in advance and let me know if anything needs to be clarified!
Not an answer as you haven't provided enough information, but some hints on where things might be going wrong.
You really should use semi-colons to end expressions, whoever left you this code to maintain needs to be reminded of their obligations to others.
One issue in your code is that currMonth is an empty string:
var currMonth = "";
...
if (eachRow > 8)
{
var date = csvdata[eachRow][2].split("-")
var month = date[0]
// This expression must always return false since currMonth == ""
if((currMonth != "") && (month.localeCompare(currMonth) != 0)) {
theRow[2] = formattedMonth
newOrders.push(theRow)
theRow = []
// this statement expression will never be executed,
// so currMonth remains an empty string
currMonth = month
for (var ci = 0; ci < columns; ci++){
// Reset data structure
theRow.push("0")
}
}
Also looking at:
for (var eachRow in csvdata){
if (eachRow>8)
presumably eachRow is a string, comparing it to a number using > will always return false if Number(eachRow) does not return a number (e.g. it will evaluate correctly if eachRow is a string like "12" or "6" but not a string like "2017-08-28").
You need to supply a sample for csvdata and the output you expect from processing it.
Spreadsheet-1: Data present in Spreadsheet-1,
Name apple android windows linux
Germany 3 4 6 7
America 4 1 6 2
Sweden 1 6 1 6
Paris 5 0 2 4
Spreadsheet-2: Data present in Spreadsheet-2,
Date Name apple android windows linux
I am able to copy the data from spreadsheet1 to spreadsheet2 by using the below google script.
function Daily() {
var SpreadSheetKeyA = "mykey1";
var SpreadSheetKeyB = "mykey2";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getSheetByName("Showstopper");
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getSheetByName("Daily");
var data = sheet1.getRange(5,11,40,6).getValues();
var time = new Date ().toJSON().slice(0,10);;
for (var r = 0; r < data.length; r++) {
data[r].unshift(time);
sheet2.appendRow(data[r]);
}
}
The data present in spreadsheet1 is dynamic i.e, the number of rows can vary. Now whenever I run the script again so as to update the spreadsheet2 data is being appended after blank rows. I would like enhance the above script so that it should avoid blank rows or only copy rows with data.
Can anyone help me with this please
This should work if all blanks are at the bottom of the source and there is nothing else below the data.
function triggerOnTime() {
var SpreadSheetKeyA = "MY key";
var SpreadSheetKeyB = "MY key";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getSheetByName("Source Name");
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getSheetByName("Target Name");
var startRow = 5;
var data = sheet1.getRange(startRow,11,sheet1.getLastRow() - startRow + 1,5).getValues();
var time = new Date ().toJSON().slice(0,10);;
for (var r = 0; r < data.length; r++) {
data[r].unshift(time);
}
if(data.length > 0 ) {
sheet2.insertRowsAfter(sheet2.getLastRow(), data.length);
sheet2.getRange(sheet2.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
}
If for whatever reason the above conditions cannot be met and you need to hardcode the number of rows to 40 you might try the following:
function triggerOnTime() {
var SpreadSheetKeyA = "MY key";
var SpreadSheetKeyB = "MY key";
var sheet1 = SpreadsheetApp.openById(SpreadSheetKeyA).getSheetByName("Source Name");
var sheet2 = SpreadsheetApp.openById(SpreadSheetKeyB).getSheetByName("Target Name");
var startRow = 5;
var data = sheet1.getRange(5,11,40,6).getValues();
var time = new Date ().toJSON().slice(0,10);;
for(var i = data.length - 1; i > -1; i--) {
if(data[i].join("").length == 0) data.splice(i, 1);
}
for (var r = 0; r < data.length; r++) {
data[r].unshift(time);
}
if(data.length > 0 ) {
sheet2.insertRowsAfter(sheet2.getLastRow(), data.length);
sheet2.getRange(sheet2.getLastRow()+1, 1, data.length, data[0].length).setValues(data);
}
}
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]);
}
}
}
I have a column of cells in a particular sheet of Google Spreadsheet document.
This column references multiple values in another sheet using the built-in JOIN command:
=JOIN(", ",Regular!B3,Regular!B9,Regular!B10,Regular!B11,Regular!B12,Regular!B13,Regular!B14)
typical output for each such cell is a list of integers that are comma-separated, f.ex:
2, 5, 10, 12, 13
Some cells use ranges like this:
=JOIN(", ",Regular!B3:B9)
I want to lock these cells in the formula as such: Regular!$B$3,Regular!$B:$9...
Right now I want each reference to lock both column and row, but a solution that lets me pick row, column or both is a better solution.
1) I haven't found a way to do this without using a custom script - have I missed something?
2) My custom script solution is unfinished:
function eachCellInRange(range, op) {
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
op(range.getCell(i,j), i, j);
}
}
};
function lockCell(cell, row, col) {
var formula = cell.getFormula();
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
//!! REGEX HERE !! //
cell.setValue(formula);
}
}
}
function lockRows() {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
eachCellInRange(range, lockCell);
};
I need to make a regex pattern that will identify the B3, B9... parts of the formula and change them to $B$3, $B$9... but also not break in the B1:B8 case
Currently all references are prefixed with SheetName! (e.g. Regular!B9:B20), in the future some may not be, so the most general solution is preferred.
I'm not sure whether this is what you're looking for but I would replace the little bit you currently have:
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
//!! REGEX HERE !! //
cell.setValue(formula);
}
}
by
if(formula.substring(0,6) == "=JOIN(") {
formula = formula.replace(/([A-Z]+(?=[0-9]))/g, function($1) {
return "$" +$1 + "$";
});
alert(formula);
// cell.setValue(formula);
}
Which ensures that the formula is a JOIN formula.
Also, I'm not that familiar with JS, but I put it in JSFiddle to see how it goes.
Warning: This will fail if your sheet names have alphanumeric characters (mix of letters and digits).
Using #Jerry's useful answer, I was able to suit it to my needs:
function eachCellInRange(range, op) {
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
op(range.getCell(i,j), i, j);
}
}
};
var lockOn = 1, lockOff = -1, lockNop = 0,
lockChar = '$', lockEmpty = '';
function lock2char(newLock, curLock) {
if(newLock == lockNop) newLock = curLock;
return (newLock > lockNop) ? lockChar : lockEmpty;
}
function bool2lock(boolValue) {
return (boolValue) ? lockOn : lockOff;
}
function lockCell(lockCol, lockRow, cell, row, col) {
var formula = cell.getFormula();
if(formula) {
var startIdx = formula.indexOf('(');
if(startIdx > 0) {
var newFormula = formula.replace(/([A-Z|\$]+(?=[0-9]))/g, function(part) {
var prefix = lock2char(lockCol, (part.charAt(0) == lockChar));
var suffix = lock2char(lockRow, (part.charAt(part.length -1) == lockChar));
part = part.replace(/\$/g, '');
return prefix + part + suffix;
});
cell.setFormula(newFormula);
}
}
}
function lockRows() {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
eachCellInRange(range, lockCell.bind(this, lockOff, lockOn));
};