Delete blank rows in spreadsheet using Google script - javascript

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);
}
}

Related

Google spradsheets scripts: randomly fill a cell from input

I have a sheet document, where people will input their name and it will randomly assign them to a group.
My intention was to do it through a button that will request an input and randomly assign a cell. For that i used:
function displayPrompt() {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt("Please enter your name");
Logger.log(result.getResponseText());
}
Now i need to assign that input to a random group and cell in that group in a specific range e.g. (C10:C16, F10:F16, I10:I16), so there are 3 groups each with 6 cells
Any help?
EDIT:
I got close with this script:
function displayPrompt() {
var ui = SpreadsheetApp.getUi();
var result = ui.prompt("Please enter your name");
var response = result.getResponseText();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("E12:G18");
var values = range.getValues();
var rangeGroups = [[], [], []];
var emptyCells = [];
for (var row = 0; row < values.length; row++) {
for (var col = 0; col < values[row].length; col++) {
var cellValue = values[row][col];
if (cellValue == "") {
emptyCells.push([row, col]);
} else {
var group = Math.floor(col / 3);
rangeGroups[group].push(cellValue);
}
}
}
var randomGroup = Math.floor(Math.random() * 3);
var randomCell = Math.floor(Math.random() * emptyCells.length);
var cellCoordinates = emptyCells[randomCell];
range.getCell(cellCoordinates[0], cellCoordinates[1]).setValue(response);
Logger.log(response);
}
But it only fills 2 columns (groups) and then throws an error probably when it wants to fill the 3rd one

Remove Duplicate based on 2 columns in Google sheets

This code removes the duplicate in the column name called "Receipt Number"
How to modify this code if the conditions matches to two column as duplicate. (i.e. Receipt Number Column and Mobile Number Column should be duplicate at the same time)
function removeDuplicates() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Records");
var vA=sh.getDataRange().getValues();
var hA=vA[0];
var hObj={};
hA.forEach(function(e,i){hObj[e]=i;});//header title to index
var uA=[];
var d=0;
for(var i=0;i<vA.length;i++) {
if(uA.indexOf(vA[i][hObj['Receipt Number']])==-1) {
uA.push(vA[i][hObj['Receipt Number']]);
}else{
sh.deleteRow(i+1-d++);
}
}
}
Thanks in advance.
I believe your goal is as follows.
You want to remove the duplicated rows by using the condition of 2 columns.
For example, when the columns of Receipt Number and Mobile Number are as follows, you want to delete the following 2 rows (4 and 5).
1 Receipt Number Mobile Number
2 a2 b2
3 a3 b3
4 a2 b2 <--- delete
5 a3 b3 <--- delete
6 a2 b3
7 a3 b2
If my understanding is correct, how about the following modification? In this modification, removeDuplicates is used.
Modified script:
function removeDuplicates() {
var checkHeadeTitles = ["Receipt Number", "Mobile Number"]; // This header titles are from your question.
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Records");
var header = sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
var columnNumber = checkHeadeTitles.map(e => {
var idx = header.indexOf(e);
if (idx == -1) {
throw new Error(`No header tilte of '${e}'.`);
}
return idx + 1;
});
sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).removeDuplicates(columnNumber);
var row = sh.getDataRange().getValues().findIndex(r => r.join("") == "");
if (row == -1) return;
sh.deleteRow(row + 1);
}
When this script is run to the above sample sheet, rows 4 and 5 are deleted.
References:
map()
removeDuplicates(columnsToCompare)
function removeDuplicates() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Records");
var [hA, ...vA] = sh.getDataRange().getValues();
var idx = {};
hA.forEach(function (e, i) { idx[e] = i; });
var uA = [];
var d = 0;
for (var i = 0; i < vA.length; i++) {
let x = `${vA[i][idx['Receipt Number']]}-${vA[i][idx['Receipt Number']]}`
if (!~uA.indexOf(x)) {
uA.push(x);
} else {
sh.deleteRow(i + 2 - d++);
}
}
}

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){}
}
}

Issues with For and MailApp

I have a code that I have compiled from some other codes but it is not doing quite what I want. The idea is to cycle through each row searching for a keyword (TRUE) and send an email to the email address listed in column A with the message in column B. Unfortunately, I'm not skilled enough to work this through myself.
function findAndSendMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reminder');
var search = "TRUE"
var lastRow = ss.getLastRow();
var range = ss.getRange(1,5,lastRow); //define range for column E
//find all occurrences of "TRUE" in column E and push range to array
var ranges = range.createTextFinder(search).findAll();
var message = '';
//loop through each range
for (i = 0; i < ranges.length; i++) {
var row = ranges[i].getRow();
var lastCol = ss.getLastColumn();
var values = ss.getRange(row, 1, 1, lastCol).getValues(); //get all values for the row
var emailAddress = values[0][0]; //column A
var reminder = values[0][1]; //column B
var sendvalidation = values[0][4]; //column E
if (sendvalidation = true) {
message+=Utilities.formatString("**This is an automated message.**\n\n"+reminder+"\n\n**This is an automated message.**");
}
var subject = 'General Reminder';
if (message) {
MailApp.sendEmail(emailAddress, subject, message);
}
}
}
I want one email for each row with only the information from that row. What I am currently getting is one email with the first row, then another email with the first and second row, then another email with the first, second and third row, etc.
Based on the question, it sounds like you have a spreadsheet and you want it to send the content in col B to the recipient in col A if col E = "TRUE". I'm a bit of a novice myself, but here is how I would approach this situation. What we're doing here is pulling all the data in the spreadsheet into an array, then looping over that array pushing rows into a new array if col E = "TRUE." We then loop over the new array, and send an email for each row with its data.
`function findAndSendMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reminder');
var data = ss.getDataRange().getValues();
var emails = [];
for(var i = 0; i < data.length; i ++) {
// Make sure this points at the column you want to check
if(data[i][4] === "TRUE") {
emails.push(data[i]);
}
}
for(var j = 0; j < emails.length; j ++) {
var row = emails[j];
var emailAddress = row[0];
var reminder = row[1];
if(reminder !== "") {
var message = "**This is an automated message.**\n\n"" + reminder + "\n\n**This
is an automated message.**";
var email = {
to: emailAddress,
subject: "General Reminder",
body: message
};
MailApp.sendEmail(email);
}
}
} `
Figured it out on my own. The code I stole from was set up differently, so I was able to get it to work by removing some conditionals.
function findAndSendMail() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Reminder');
var search = "TRUE"
var lastRow = ss.getLastRow();
var range = ss.getRange(1,5,lastRow); //define range for column E
//find all occurrences of "TRUE" in column E and push range to array
var ranges = range.createTextFinder(search).findAll();
//loop through each range
for (i = 0; i < ranges.length; i++) {
var row = ranges[i].getRow();
var lastCol = ss.getLastColumn();
var values = ss.getRange(row, 1, 1, lastCol).getValues(); //get all values for the row
var emailAddress = values[0][0]; //column A
var reminder = values[0][1]; //column B
var sendvalidation = values[0][4]; //column E
var message = reminder;
var subject = 'General Reminder';
MailApp.sendEmail(emailAddress, subject, "**This is an automated message.**\n\n"+message+"\n\n**This is an automated message.**");
}
}

How can I change the background color for only the rows that are "true"?

How can I change the background color for only the rows that were (true) as per function checkDate(row) on the originating sheet "Pasco"? Is this possible?
A little bit about the script:
A date range is inputted through function getDateRange(), all rows in sheet "Pasco" is checked for if they meet that date range through function checkDate(row). If it does meet the date range (true), function filterRows() essentially filters the rows from "Pasco" sheet, and moves them over to another sheet "Copy of Pasco".
Another way of asking my question, how can I get a range of all the rows that were "true" in sheet "Pasco". If "Pasco" wasn't sorted by date, this could mean multiple ranges, right? Once I have a range I'd be able to change background easy.
If you are to test the script, please create two sheets, 'Pasco' and 'Copy of Pasco'. In 'Pasco' Starting from row 2, place some dates down column I (column 8). To see the filtering in action. 'Copy of Pasco' will be deleted/created on each run.
Thank you for your time =)
var globalStartDate;
var globalEndDate;
function getDateRange(){
var startui = SpreadsheetApp.getUi();
var startprompt = startui.prompt('Start Date', 'Enter a date in m/d/y format', startui.ButtonSet.OK_CANCEL);
var startdate = new Date(startprompt.getResponseText());
var startdatemilliseconds = startdate.getTime();
Logger.log(startdate);
Logger.log(startdatemilliseconds);
globalStartDate = startdatemilliseconds;
var endui = SpreadsheetApp.getUi();
var endprompt = endui.prompt('End Date', 'Enter a date in m/d/y format', endui.ButtonSet.OK_CANCEL);
var enddate = new Date(endprompt.getResponseText());
var enddatemilliseconds = enddate.getTime();
Logger.log(enddate);
Logger.log(enddatemilliseconds);
globalEndDate = enddatemilliseconds;
}
function checkDate(row) {
Logger.log(row[8].getTime() <= globalEndDate && row[8].getTime() >= globalStartDate);
return (row[8].getTime() <= globalEndDate && row[8].getTime() >= globalStartDate); // Check column H
}
function filterRows() {
var Spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = Spreadsheet.getSheetByName('Pasco');
var sheetdelete = Spreadsheet.getSheetByName('Copy of Pasco');
Spreadsheet.deleteSheet(sheetdelete);
Spreadsheet.setActiveSheet(sheet1);
Spreadsheet.duplicateActiveSheet();
var headers = 1; // # rows to skip
var sheet2 = Spreadsheet.getSheetByName('Copy of Pasco');
var range = sheet1.getDataRange();
var data = range.getValues();
var headerData = data.splice(0,headers); // Skip header rows
getDateRange();
var filteredData = data.filter( checkDate );
var outputData = headerData.concat(filteredData); // Put headers back
Logger.log(filteredData)
sheet2.clearContents(); // Clear content, keep format
// Save filtered values
sheet2.getRange(1, 1, outputData.length, outputData[0].length).setValues(outputData);
}
Sorry I don't have time to read through your code and give you a complete answer but you could just add a loop to go through the sheet and set the background colour of each row with 'true'.
In my script below I assume 'true' is in column A.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var data = sheet.getRange(1, 1, sheet.getLastRow()).getValues();
var lastCol = sheet.getMaxColumns();
for (var i = 0; i < data.length; i ++){
if(data[i][0] == true){
sheet.getRange(i + 1, 1, 1, lastCol).setBackground('Yellow');
}
}
}
EDIT
Insert this code after you call getDateRange() in the filter rows function.
var lastCol = sheet1.getMaxColumns();
for(var i = headers; i < data.length ; i++){
if(data[i][8].getTime() <= globalEndDate && data[i][8].getTime() >= globalStartDate){
sheet1.getRange(i, 1, 1, lastCol).setBackground('Yellow');
}
}
Your filter rows function should now look like this:
function filterRows() {
var Spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = Spreadsheet.getSheetByName('Pasco');
var sheetdelete = Spreadsheet.getSheetByName('Copy of Pasco');
Spreadsheet.deleteSheet(sheetdelete);
Spreadsheet.setActiveSheet(sheet1);
Spreadsheet.duplicateActiveSheet();
var headers = 1; // # rows to skip
var sheet2 = Spreadsheet.getSheetByName('Copy of Pasco');
var range = sheet1.getDataRange();
var data = range.getValues();
var headerData = data.splice(0,headers); // Skip header rows
getDateRange();
var lastCol = sheet1.getMaxColumns();
for(var i = headers; i < data.length ; i++){
if(data[i][8].getTime() <= globalEndDate && data[i][8].getTime() >= globalStartDate){
sheet1.getRange(i + headers, 1, 1, lastCol).setBackground('Yellow');
}
}
var filteredData = data.filter( checkDate );
var outputData = headerData.concat(filteredData); // Put headers back
Logger.log(filteredData)
sheet2.clearContents(); // Clear content, keep format
// Save filtered values
sheet2.getRange(1, 1, outputData.length, outputData[0].length).setValues(outputData);
}

Categories

Resources