Google sheets script to export rows to different worksheet - google-apps-script

Here is a script I'm using that works, but is trying to import too much data. I apologize for my ignorance when it comes to this.
function copyPaste() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("Print Results");
var rangeSource = source.getDataRange();
var data = rangeSource.getValues();
var lr = rangeSource.getLastRow();
var lc = rangeSource.getLastColumn();
Logger.log(data);
var sss = SpreadsheetApp.openById("XXXXXX");
var target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,lr,lc).setValues(data);
}
What I need this to do is only select A8:M, and only copy over the rows that have a USER ID in column J. The USER ID is always a number, so a simple rule of ">0" should work for that. USER ID is imported from a different tab using a vlookup formula so the cell is not "empty".

Only copy rows where Column J is not null
function copyPaste() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Print Results");
const rg = sh.getDataRange();
const vs = rg.getValues().filter(r => r[9] != '');
Logger.log(vs);
const sss = SpreadsheetApp.openById("XXXXXX");
const target = sss.getSheetByName("Sheet2")
target.getRange(target.getLastRow()+1,1,vs.length,vs[0].length).setValues(vs);
}
.filter() method

Related

Copy Data from One Tab to Another without Blanks

I am trying to copy Data from One Tab to Another. Just to Demonstrate.
Sheet1 has in Column E, the following Data:
abc
def
efg
hig
DataDump Sheet should contain (notice how there are no blanks). All of this should be done via app script:
abc
def
efg
hig
This is the formula that I have so far.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheet1ByName("Sheet");
var pasteSheet = ss.getSheetByName("DataDump");
var source = copySheet.getRange(1,5,6000,1);
if(source != ""){
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,5000,1);
source.copyTo(destination);
}
}
The above formula is what I have tried so far.
Try it like this:
function copyInfo() {
const ss = SpreadsheetApp.getActive();
const sh0 = ss.getSheetByName("Sheet0");
const sh1 = ss.getSheetByName("Sheet1");
const vs = sh0.getRange(1, 5, sh0.getLastRow()).getValues().filter(e => e[0]);
sh1.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}

Copy filtered range from main sheet to specific columns in another google spreadsheet

Basically I want to filter this sheet
by column J "company" and copy paste to several sheets.
[Data Sheet]
For example, the script will filter by Column J - KMA and it will populate into this Testing sheet 1 - KMA
[Testing Sheet 1]
Then the script will filter by Column J - LOL and it will populate into this Testing Sheet 2 - LOL
[Testing Sheet 2]
Whenever I populate the testing sheets, it will key in the current date in column A.
I tried using this code and it works. But they are not appending to a new empty row.... and it loads pretty slow
function FilterByKMA() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var test1 = SpreadsheetApp.openById("Sheet").getSheetByName("Sheet"); // Actual Sheet
const range = ss.getRange("A:X");
var checkfilter = range.getFilter();
const filter = range.createFilter();
const Filter_Criteria = SpreadsheetApp.newFilterCriteria().whenTextContains(["KMA"]);
const coll1 = 10; //
const add_filter = filter.setColumnFilterCriteria(coll1,Filter_Criteria);
Logger.log("Filter has been added.");
for(var i = 2; i <= ss.getLastRow();i++){
if(ss.getRange(i,10).getDisplayValue()=="KMA")
{
var tid = ss.getRange(i,1).getDisplayValue();
console.log(tid)
var last_scan_type = ss.getRange(i,6).getDisplayValue();
var last_scan_date = ss.getRange(i,7).getDisplayValue();
var last_scan_name = ss.getRange(i,9).getDisplayValue();
var maincon = ss.getRange(i,10).getDisplayValue();
// var to = ss.getRange(i,).getDisplayValue();
var cxname = ss.getRange(i,14).getDisplayValue();
var cxcontact = ss.getRange(i,15).getDisplayValue();
var to_address = ss.getRange(i,13).getDisplayValue();
var cod_cost = ss.getRange(i,17).getDisplayValue();
var timeZone = Session.getScriptTimeZone();
var email = Session.getActiveUser().getEmail();
test1.appendRow([Utilities.formatDate(new Date(), timeZone, "dd-MMM-yyyy"),email,,tid,,,,,,to_address,cxname,cxcontact,,,,last_scan_type,last_scan_date,last_scan_name,cod_cost])
}
}
filter.remove();
}

Converting Formula Result in cell into Notes

So I understand this is very similar to:
Set Notes from Column A based on contents of Column B
function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var values_range = sheet.getRange("R5:R6");
var notes_range = sheet.getRange("AS5:AS6");
var notes = notes_range.getValues();
values_range.setNotes(notes)
}
However it's returning blank for me. It seems to be because the cells are the results of a SUM.
Is there a way to get the result?
Copy display values in column B as notes in column A
function addNote() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet0")
const vs = sh.getRange(2,2,sh.getLastRow() - 1).getDisplayValues();
vs.forEach((r,i)=>{
sh.getRange(i + 2,1).setNote(r[0])
});
}

How to compare values from a google sheets column to an array and send to specific emails based on column data

I'm automating my google sheets to send out emails automatically and I can't figure out how to compare a variable array to a column in my google sheets. I keep getting all values returned rather than just the ones in my array when I put it in an 'IF' statement.
I want my code to only show data that is in my gEnglish variable but instead shows me all data. I was also wondering how I'd be able to send emails based on regions to users.
function sendEmails() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var decisionOne = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange(1,1).getValue();
var decisionTWO = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange(2,1).getValue();
var decisionThree = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange(3,1).getValue();
var decisionFour = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange(5,1).getValue();
var decisionFive = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange(4,1).getValue();
var gEnglish = ["IT","US","GB","UK"];
var gSpanish = ["SP"];
var user1 = ["JP","SK","IND","RU"];
var user2 = [gEnglish,"UK"];
var user3 = [gSpanish, "BR", "PT"];
var user4 = ["FR", "DE","IT"];
for (var i = 2;i<=lr;i++){
var type = ss.getRange(i,6).getValue();
var region = ss.getRange(i, 5).getValue();
var decision = ss.getRange(i,15).getValue();
//I used to have if(region == gEnglish)
if(gEnglish.includes(region[i]) ){
var channel = ss.getRange(i,3).getValue();
var decision = ss.getRange(i,15).getValue();
var url = ss.getRange(i, 9).getValue();
var name = ss.getRange(i,11).getValue();
var messageBody = {};
var subjectLine = "blahblahblah";
}
}
}
If my array only has one value inside it works fine but as soon as I put more I just get all values returned.
What am I doing wrong?
Putting a column into a flat array.
function col2array() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const vA=sh.getRange(1,1,sh.getLastRow(),1).getValues().flat();
}
In Google Apps Script a column looks like:
[[Row1],[Row2],[Row3],[Row4]....]
function lookingforarrayvaluesinacolumn() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet1');
const vA=sh.getRange(1,1,sh.getLastRow(),1).getValues();
const coolpeople = ["p1","p2"];
vA.forEach(function(r,i){
if(coolpeople.indexOf(r[0])!=-1) {
//the r[0] is a cool person
}
});
}

How to overwrite cell function with Paste value only using Google Script

I have a database that makes live calculations, but once the new data added to the database has been processed that day, I effectively want to Copy + Paste value only on these cells so that the results of the formulas are fixed and can no longer change.
The current script I am using is:
function myFunction() {
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getActiveSheet();
var r = ss.getRange(1,2,ss.getLastRow());
var vals = r.getValues();
for(var i =0;i<vals.length;i++){
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
However, in column B beneath the data set, all the cells start with IF(B2092="","",etc). On cells that are empty, I still require the formula to remain in place. I only want to script to be applied to cells that have data within.
Can anyone help with this? Thanks
Try this:
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = formulas.map((row,row_index) =>
row.map((cel, cel_index) =>
{ return (cel) ? cel : vals[row_index][cel_index] }));
rg.setFormulas(data); // output
}
Corrected version:
function copy_values_and_formulas() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getValues(); // get values
var formulas = rg.getFormulas(); // get formulas
// merge values & formulas
var data = vals.map((row, row_index) =>
row.map((cel, cel_index) =>
{ return (cel == 0) ? formulas[row_index][cel_index] : cel }));
rg.setFormulas(data); // output
}
function myFunction() {
var sh = SpreadsheetApp.getActive();
var ss = sh.getActiveSheet();
var rg = ss.getRange(1,2,ss.getLastRow());
var vals = rg.getDisplayValues();
for(var i=0;i<vals.length;i++){
if(vals[i][1] && vals[i][1].length!=0) {
ss.getRange(i+1,2).setValue(ss.getRange(i+1,2).getValue());
}
}
}