The code that was given to me works by saving web scraped data in google sheets, however, when it saves data it creates duplicates of the same data entry from previously scheduled web scrapes. Is there a way we can create a code where it only saves unique rows and adds in edited/ updated info? I have added the code below:
function myFunction() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue('=importdata("https://www.parsehub.com/api/v2/projects/tZOywrNXQ3Q4/last_ready_run/data?api_key=tn6CGEyTTVxE&format=csv")')
}
// custom menu function
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu').addItem('Save Data','saveData').addToUi().
}
// function to save data
function saveData() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh4=ss.getSheetByName('Zapier Tax Leads');
var sh3=ss.getSheetByName('Current Tax Leads')
var data=sh3.getRange(1,1,sh3.getLastRow(),33).getValues();
sh4.getRange(sh4.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
function removeDuplicates() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Zapier Tax Leads')
var data = sheet.getDataRange().getValues();
var newData = new Array();
for(i in data){
var row = data[i];
var duplicate = false;
for(j in newData){
if(row.join() == newData[j].join()){
duplicate = true;
}
}
if(!duplicate){
newData.push(row);
}
}
sheet.clearContents();
sheet.getRange(1, 1, newData.length,
newData[0].length).setValues(newData);
}
Edit:
The tab labeled "Zapier Tax Leads" is where the code copies over the duplicate data.
In tab "Current Tax Leads" the columns that we will be updating are only column H through O.
Spreadsheet link: https://docs.google.com/spreadsheets/d/1G1PLs46cnQ-CyJWBI0ibKNmYosSEQRcrAGE8Qx2MArU/edit?usp=sharing
Not the exact solution but the sheet-api's DeleteDuplicatesRequest should work.
I have not tested it, but this should work. (I presume you use the drive package for nodejs?)
var requests = {
"requests": [{
"deleteDuplicates": {
//rows to operate on
//first value is kept if there are duplicates
"range": {
"sheetId": integer,
"startRowIndex": integer,
"endRowIndex": integer,
"startColumnIndex": integer,
"endColumnIndex": integer
}
,
// The columns in the range to analyze for duplicate values.
// If no columns are selected then all columns are
// analyzed for duplicates.
"comparisonColumns": [{
"sheetId": integer,
"dimension": "COLUMNS",
"startIndex": integer,
"endIndex": integer
}]
}
}]
}
sheets.spreadsheets.values.batchUpdate(spreadsheetId=spreadsheet_id,body=requests)
Related
I manage a large email list for my gaming society. In column A, I have the e-mails, and in column B, I have the usernames. I populate column B with a formula that extracts the name of the user from their e-mail address, which is often in the firstname.lastname#email.com form. Column B therefore returns "Firstname" after I run the formula if the user's email is in the firstname.lastname#email.com format.
Sometimes, however, the emails have just the initial of the first name (f.lastname#email.com) and, in these case, I want to have Column B return the word 'Gamer' rather than, for example, the first letter of the user's email.
Here is the script I use at the moment, which current deletes all rows with four or more numbers:
function removeNumbers() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[0].toLowerCase().indexOf("robot") > -1) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
On your shared spreadsheet, use this
function firstName() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var lr=sheet.getLastRow()
var val = sheet.getRange(2,1,lr-1,2).getValues();//get the col A email addrs
var newVal=[]//new array for before fitst period
for (var i = 0; i <= val.length-1; i++) {
var str=val[i][0].split(".")//split email at period
var len=str[0].length // determine lenght of number string of first split
if(val[i][1]=="inactive"){
newVal.push(["inactive"])
continue
}
if(len<=1){//if 1 or less
def="Gamer"
newVal.push([def]) //put Gamer in new array
}
else{
newVal.push([toTitleCase(str[0])]) //keep first name
}}
sheet.getRange(2, 2, newVal.length, 1).setValues(newVal)//set new values in col B
}
function toTitleCase(str) {
return str.replace(/\w\S*/g, function (txt) {
return txt.charAt(0)
.toUpperCase() + txt.substr(1)
.toLowerCase();
});
}
This well also run on the active sheet.
Column A has a Timestamp
Column B has a string of numbers and letters that may be duplicated
Column C has other data associated with the data in A and B
How can I delete a row that contains duplicates in column B and leave the LAST entry (the one with the newest Timestamp)
It doesn't have to be deleted per se, I can pull the data into a new column if needed. I just want unique values with the latest timestamp.
This should do it for you.
function findCopyDeleteDupes()
{
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sht=ss.getActiveSheet();
var rng=sht.getDataRange();
var rngA=rng.getValues();
var nodupes=[];
var dupeRows=[];
for(var i=0;i<rngA.length;i++)
{
var idx=nodupes.indexOf(rngA[i][1]);
if(idx==-1)
{
nodupes.push(rngA[i][1]);
}
else
{
for(var j=0;j<rngA[i].length;j++)
{
rngA[idx][j]=rngA[i][j];
}
dupeRows.push(i+1);
}
}
rng.setValues(rngA);
for(var i=dupeRows.length-1;i>-1;i--)
{
sht.deleteRow(dupeRows[i])
}
}
Say, I want to delete A1:A10 from sheet 1 to sheet 30, where each cell contains different numbers. How to do it all at a time?
Can you please help me with the error:
This is a multisheet delete you can setup the object delO to delete the same range or different ranges in any of the sheets on a spreadsheet
if you want them all to be the same then enter the same ranges
function multiSheetDelete()
{
var delO={sheet1:'A3',sheet2:'A1:A10',Sheet5:'A2:A20',Sheet6:'A1:A4'};
var keys=getKeys(delO);
var ss=SpreadsheetApp.getActiveSpreadsheet();
var allSheets=ss.getSheets();
for(var i=0;i<allSheets.length;i++)
{
var sht=allSheets[i];
var shtnam=sht.getName();
if(keys.indexOf(shtnam)>-1)
{
var rng=sht.getRange(delO[shtnam]);
rng.clearContent();
}
}
}
function getKeys(aArray)
{
var keys = [];
for(var key in aArray)
{
if(aArray.hasOwnProperty(key))
{
keys.push(key);
}
}
return keys;
}
I have a few scripts which, individually, all work. However I am unable to get them to all run on opening the sheet. At the moment, only the first script (hideColumn) runs on opening the sheet. The others all work when called directly from the menu I managed to create.
function onOpen() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// create menu
var menu = [{name: "Hide Overview Columns", functionName: "hideColumn"},
{name: "Hide Rebalancing Columns", functionName: "hideColumn2"},
{name: "Add/Hide Dividend Data for Chart", functionName: "hideColumn3"}];
// add to menu
ss.addMenu("Check", menu);
// execute function
function hideColumn(){
function hideColumn(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Overview");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(1, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}
function hideColumn2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Rebalancing");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(2, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}
function showColumn3() {
// get active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Dividends - Calculations");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn();
// show all columns
sheet.showColumns(1, lastCol);
}
function hideColumn3() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Dividends - Calculations");
// get data
var data = sheet.getDataRange();
// get number of columns
var lastCol = data.getLastColumn()+1;
Logger.log(lastCol);
// itterate through columns
for(var i=1; i<lastCol; i++) {
if(data.getCell(1, i).getValue() == 'HIDE') {
sheet.hideColumns(i);
}
}
}}}
we can help but won't code for you unless you showed us you tried!
Your best option is to have a look at the Google Apps Script documentation.
Here are the pages you need to check to do your script (you'll see it's well explained)
Create a menu
Hide a column
Get the content of a range
Create a loop to check several cell one by one in JS (Mozilla Doc)
I want to hide all rows in a google drive spreadsheet that do not have any value in the cells in Column A. Then be able to view them again when needed. What is the script/formula for this function? Thanks
UPDATED to include a menu to Show all Rows:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(1, 1, sheet.getLastRow());
function onOpen() {
//add menu called Visibility onOpen
ss.addMenu("Visibility", [{
name: "Show All Rows", functionName: "showAllRows"
}]);
//get the values to those rows
var values = range.getValues();
//go through every row
for (var i=0; i<values.length; i++){
//if row value is equal to empty
if(values[i][0] === ""){
//hide that row
sheet.hideRows(i+1);
}
}
}
function showAllRows(){
sheet.showRows(1,sheet.getLastRow());
}
I've modified Blexy's menu to allow hiding of empty rows as a menu item rather than doing so during onOpen.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0]; //Sheet1 is in Array[0]
var range = sheet.getRange(1, 1, sheet.getLastRow());
function onOpen() {
//add menu called Visibility onOpen
ss.addMenu("Visibility",
[{
name: "Show All Rows", functionName: "showAllRows"
},
{
name: "Hide All Empty Rows", functionName: "hideAllEmptyRows"
}
]
);
}
function showAllRows(){
sheet.showRows(1,sheet.getLastRow());
}
function hideAllEmptyRows(){
//get the values to those rows
var values = range.getValues();
//go through every row
for (var i=0; i<values.length; i++){
//if row value is equal to empty
if(values[i][0] === ""){
//hide that row
sheet.hideRows(i+1);
}
}
}