Google Apps Script - copyrange based on for loop matching - javascript

https://docs.google.com/spreadsheets/d/1wAxZROq-HqGSZIeBnOShiLP1x-maCpVhvfr6MKixVZE/edit#gid=1767460404
I am attempting to grab the data in a data range on a data sheet, then loop over that data, match the row values in Column C to the row values in a different (tracker) sheet also located in Column C, and for the matched data, copy the whole row in data sheet to the bottom of the data sheet.
All of this is supposed to be triggered when I enter "Open" in a set field in the status tracker.
What I have done so far (hopefully) is to locate the values to match in the data sheet but I am unsure as to how to use the .copyto function correctly now such that it copys the data to the end of the loop. My code so far: (without .copy.
function grabmyRequestID(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var ss2 = ss.getSheetByName("Status Tracker")
var searchforrange = ss2.getRange("C:C")
var searchfor = searchforrange.getValues()
Logger.log("Request ID: " +searchfor)
return searchfor
}
function findDuplicates(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var ss2 = ss.getSheetByName("Status Tracker")
var ss3= ss.getSheetByName("All Output")
var sheetNametoWatch = "Status Tracker"
var columntoWatch = 4
var valuetoWatch = "Open"
var searchfor = grabmyRequestID()
if (sheet.getName() == sheetNametoWatch && range.getColumn()
== columntoWatch && range.getValue() == valuetoWatch){
var data = ss3.getRange("C:C").getValues()
for (i=0; i<data.length; i++){
if (data[i] == searchfor && searchfor !=""){
Logger.log('found it')
}
}
}
}
As you can see, I am running two functions, grabmyRequestID, which grabs the id from the tracking sheet and findduplicates, which matches it with the ids from the data sheet.
I now just want it to copy the whole row where it found the matched column and copy that row to the end of the data sheet. I have tried a few ways but I never actually get any output. I am pretty new to this so I hope somebody can help me out.
I am also not sure entirely sure if my trigger works for this loop.
Any help would be appreciated.

Related

How do I replace a value in one cell based on value in another cell?

function CopyinData_AM_A() {
/* Edit the vars below this line for your needs */
var sourceSheet = "Students AM" ; // Enter the name of the sheet with the source data
var sourceRange = "B7:N77" ; // Enter the range of the cells with the source data
var targetSheet = "Students AM A" ; // Enter the name of the target sheet
var targetRange = "B7:N77" ; // Enter the range of cells you wish to copy data to. Note this must be same size as source range.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getRange(sourceRange).getValues();
ss.getSheetByName(targetSheet).getRange(targetRange).setValues(values);
}
How would I add this into the above script? The action needs to occur after the data is copied into the sheet. I am only a VB coder. 'Students AM A' is a formula free sheet.
For each cell in ‘Students AM A'!N7:N77 then
If Ncell= ‘Menu!D14' then Hcell = ”O"
Check next cell
End
I'd have zero issue doing this in VB, I just started using google scripts about 3 months ago. Anyone recommend a good book to assist me in learning this google script stuff?
Once you have a value range, you can loop through the rows and compare the values against the reference value
You can use e.g. a for loop, a conditonal if statement and the == comparison operator
Sample to be merged with the rest of your function:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("Students AM");
var referenceSheet = ss.getSheetByName("Menu");
var referenceValue = referenceSheet.getRange("D14").getValue();
var sourceRange = "B7:N77";
var values = sourceSheet.getRange(sourceRange).getValues();
for (var i = 0; i < values.length; i++){
// values[i][12] correspond to the values in column N of each row
if(values[i][12] == referenceValue){
sourceSheet.getRange((sourceRange + i), 7).setValue("O");
}
}
For further reading:
getSheetByName()
getRange(a1Notation)
getRange(row, column)
getValue()
setValue()

Script to copy multiple ranges to another sheet (Google Sheets)

Pretty new to coding and I've been trying to solve this one, but I can't find out how and now I'm turing to you guys:
There is a spec sheet, containing a couple of information on an item:
Product Name (on A3)
Product Code (on A4)
Volume (on c3)
Und (on C4)
Cost (on A5), etc.
Then, underneath this "header", there is a table containing the ingredients that make up that item:
ItemNumber, Product, Manufacturer, Unt, Cost, Remarks
These are set on a range (A12:T50)
The need is to have this table moved to a sheet, which the below code does, but how I can I have the header moved as well, repeating each of the header ranges, forming a database-like table for further consulting?
Here is the working script:
function copyrange() {
var sourceSheet = 'New Item';
var destinationSheet = 'Consolidated';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var LastRowSource = sheet.getLastRow();
var LastColumnSource = sheet.getLastColumn();
var values = sheet.getRange(11,1,LastRowSource,LastColumnSource).getValues();
var csh = ss.getSheetByName(destinationSheet);
var data = [];
var j =[];
for (var i = values.length-1; i != ""; i--) {
if ( values[i][0] != '') {
data.push(values[i]);
//sheet.deleteRow(i+1)
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Here is the link to the file in question:
https://docs.google.com/spreadsheets/d/1GlD_VIOFHj7PGfCCnfVZqdZbE22aVoUw91BSCZecsRc/edit#gid=371161645
Any help is appreciated.

Paste values from one Google Sheet to another and remove duplicates based on ID column

I have a similar situation to the one described on this question: two worksheets, with input data coming into the Feed sheet using the importxml function and a Data sheet where new rows get copied thanks to a script set to run daily.
However, the current script is creating daily duplicates. As such, I would like to adapt the answer provided on the question above so that the script checks the IDs on column F and only copies the rows with new IDs.
How should I update the section below that creates a hash to one that looks for the IDs on column F instead? Also my rows are consistent, so is it correct to assume I can just remove the relevant code lines towards the end?
The sample Google Sheet is available here.
function appendUniqueRows() {
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('Get Data');
var destSheet = ss.getSheetByName('Final Data');
var sourceData = sourceSheet.getDataRange().getValues();
var destData = destSheet.getDataRange().getValues();
// Check whether destination sheet is empty
if (destData.length === 1 && "" === destData[0].join('')) {
// Empty, so ignore the phantom row
destData = [];
}
// Generate hash for comparisons
var destHash = {};
destData.forEach(function(row) {
destHash[row.join('')] = true; // could be anything
});
// Concatentate source rows to dest rows if they satisfy a uniqueness filter
var mergedData = destData.concat(sourceData.filter(function (row) {
var hashedRow = row.join('');
if (!destHash.hasOwnProperty(hashedRow)) {
// This row is unique
destHash[hashedRow] = true; // Add to hash for future comparisons
return true; // filter -> true
}
return false; // not unique, filter -> false
}));
// Check whether two data sets were the same width
var sourceWidth = (sourceData.length > 0) ? sourceData[0].length : 0;
var destWidth = (destData.length > 0) ? destData[0].length : 0;
if (sourceWidth !== destWidth) {
// Pad out all columns for the new row
var mergedWidth = Math.max(sourceWidth,destWidth);
for (var row=0; row<mergedData.length; row++) {
for (var col=mergedData[row].length; col<mergedWidth; col++)
mergedData[row].push('');
}
}
// Write merged data to destination sheet
destSheet.getRange(1, 1, mergedData.length, mergedData[0].length)
.setValues(mergedData);
}
I'm a novice in this world of Google Apps scripts, so do please let me know if I'm missing any crucial information. Thanks in advance for the help.
You want to copy the values from "Feed" sheet to "Data" sheet.
When the values are copied, you want to copy only new values which are not included in "Data" sheet.
You want to choose the new values using the values of column "F".
If my understanding for your question is correct, how about this modification? In this modification, I modified the script in your shared spreadsheet.
Modification points:
In your script, all values of "Feed" sheet are copied to "Data" sheet. So in order to choose only new values, I used the following flow.
Retrieve the values from column "F". This is used for choosing the new values.
Retrieve the new values using the values from column "F".
Put the new values to "Data" sheet.
The script which reflected above flow is as follows.
Modified script:
From:
This is your script in the shared spreadsheet. Please modify this script to below one.
function Copy() {
var sss = SpreadsheetApp.openById('#####'); // this is your Spreadsheet key
var ss = sss.getSheetByName('Feed'); // this is the name of your source Sheet tab
var range = ss.getRange('A3:H52'); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('#####'); //replace with destination ID
var ts = tss.getSheetByName('Data'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1,50,8).setValues(data);// 49 value refers to number of rows, 8 to columns
}
To:
function Copy() {
var sss = SpreadsheetApp.openById('#####'); // this is your Spreadsheet key
var ss = sss.getSheetByName('Feed'); // this is the name of your source Sheet tab
var range = ss.getRange('A3:H52'); //assign the range you want to copy
var data = range.getValues();
var tss = SpreadsheetApp.openById('#####'); //replace with destination ID
var ts = tss.getSheetByName('Data'); //replace with destination Sheet tab name
// Below script was added.
var values = ts.getRange("F3:F").getValues().filter(String);
var copiedValues = data.filter(function(e) {return !values.some(function(f){return f[0] == e[5]}) && e.filter(String).length > 0});
ts.getRange(ts.getLastRow() + 1, 1, copiedValues.length, copiedValues[0].length).setValues(copiedValues);
}

Update data from a Spreadsheet - Apps Script

I'm doing a data transfer of several spreadsheets to a single one, what I do is transfer the last data of certain columns to the master spreadsheet and also insert them in the last available row of certain columns, for now, I insert all the data but I would like to to know how I can have it examine the master spreadsheet so that if those data already exist, it does not delete them but update them. The script that I have is the following ...
function Gas10(){
var ss1 = SpreadsheetApp.openById("ID");
var ssh1 = ss1.getSheetByName("Sheet 1");
var lastRow1 = ssh1.getLastRow();
var gtRange1 = ssh1.getRange("C"+(lastRow1)+":K"+(lastRow1)).getValues();
var gtRange2= ssh1.getRange("A" + (lastRow1)).getValue();
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var ssh2 = ss.getSheetByName("Sheet 2");
var lastRow2 = ssh2.getLastRow() + 1;
var setRange1 = ssh2.getRange(lastRow2, 4, gtRange1.length, gtRange1[0].length).setValues(gtRange1);
var setRange2 = ssh2.getRange(lastRow2, 3).setValue(gtRange2);
}
I need to know how I can do it when I insert a piece of information (I already do that), but update it if it already exists. This is the example that I created so that it can be better understood, in this example I have two sheets of which from sheet 1 I pass data to sheet 2 and what I'm looking for is that sheet 2 updates all the data that are equal to (Name, Num, Proyect). I hope that now I understand better what I'm looking for.
Basically what you have to do is
get the new Line you want to add to the destination spreadsheet
get all the required datas of the destination spreadsheet
Check if the new Line datas have the same datas than in the destination data array
If so change ID value
paste changed datas in the destination spreadsheet
based on this spreadsheet The code should look something like this
function Gas10(){
var ss1 = SpreadsheetApp.getActiveSpreadsheet();
var ssh1 = ss1.getSheetByName("Sheet 1");
var ssh2 = ss1.getSheetByName("Sheet 2");
var lastRow1 = ssh1.getLastRow();
var lastCol1 = ssh1.getLastColumn();
var newLine = ssh1.getRange(lastRow1, 2, 1, lastCol1 - 1 ).getValues();
var destDatas = ssh2.getDataRange().getValues();
for (var i = 1; i < destDatas.length; i++)
{
if (newLine[0][0] == destDatas[i][0]
&& newLine[0][1] == destDatas[i][1]
&& newLine[0][2] == destDatas[i][2])
{
destDatas[i][3] = newLine[0][3];
}
}
// add newLine to destDatas
destDatas.splice(destDatas.length, 0, newLine[0]);
var lastColumn = ssh2.getLastColumn();
var lastRow2 = ssh2.getLastRow() + 1;
ssh2.getRange(1, 1, destDatas.length, lastColumn).setValues(destDatas);
ssh1.deleteRow(lastRow1);
}
Here's an example I played around with:
It looks at the slave sheet for any data. When it finds data it puts the row and col and value into an obj which is then added to an array. When it finishes it calls the updMaster which then looks for data in those same cells (assuming that the cells are in the same place if those cells are blank then it adds data and I also changed the background to lightblue to show me where it updated the cells.
You could run the getSlaveData() for different sheets if you wish.
function getSlaveData(){
var ss=SpreadsheetApp.getActive();
var ssh=ss.getSheetByName('Sheet2');
var sA=[];
var srg=ssh.getDataRange();
var svA=srg.getValues();
for(var i=0;i<svA.length;i++){
for(var j=0;j<svA[i].length;j++){
//if(svA[i][j]){
if(!ssh.getRange(i+1,j+1).isBlank()){//optional way to look for values
var sObj={};
sObj['row']=i + 1;
sObj['col']=j + 1;
sObj['value']=svA[i][j];
sA.push(sObj);
}
}
}
updMaster(sA);
}
function updMaster(sA){
var ss=SpreadsheetApp.getActive();
var msh=ss.getSheetByName('Sheet1');
for(var i=0;i<sA.length;i++){
if(msh.getRange(sA[i].row,sA[i].col).isBlank()){
msh.getRange(sA[i].row,sA[i].col).setValue(sA[i].value);
msh.getRange(sA[i].row,sA[i].col).setBackground('lightblue');
}
}
}

Google Script: Copy row to another sheet on edit ALMOST THERE

I've spent hours trying to get this to work so any help is much appreciated I've got the code below copying from my main sheet to logs. However the code I'm trying to alter looks to column 10 for the sheet name. I'd like this just to default to the sheet logs any idea what needs changing to make this happen? Thanks in advance
function onEdit(e) {
// see Sheet event objects docs
// https://developers.google.com/apps-script/guides/triggers/events#google_sheets_events
var ss = e.source;
var s = ss.getActiveSheet();
var r = e.range;
// to let you modify where the action and move columns are in the form responses sheet
var actionCol = 9;
var nameCol = 10;
var target = SpreadsheetApp.openById('18L5-RGHBx41sMtvC1-7AoVCo6y3imvtELQFjkhPXtoY')
var targetsheet = target.getSheetByName('logs');
// Get the row and column of the active cell.
var rowIndex = r.getRowIndex();
var colIndex = r.getColumnIndex();
// Get the number of columns in the active sheet.
// -1 to drop our action/status column
var colNumber = s.getLastColumn()-1;
// if our action/status col is changed to yes do stuff
if (e.value == "Yes" && colIndex == actionCol) {
// get our target sheet name - in this example we are using the priority column
var targetSheet = s.getRange(rowIndex, nameCol).getValue();
// if the sheet exists do more stuff
if (ss.getSheetByName(targetSheet)) {
// set our target sheet and target range
var targetSheet = ss.getSheetByName(targetSheet);
var targetRange = targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, colNumber);
// get our source range/row
var sourceRange = s.getRange(rowIndex, 1, 1, colNumber);
// new sheets says: 'Cannot cut from form data. Use copy instead.'
sourceRange.copyTo(targetRange);
// ..but we can still delete the row after
r.setValue("Logged")
// or you might want to keep but note move e.g. r.setValue("moved");
}
}
}
It looks like you are trying to get a sheet by it's name.
The following code will return the sheet named Logs.
var targetSheet = SpreadsheetApp.getActive().getSheetByName('Logs');
Google Apps Script Documentation: getSheetByName(name)

Categories

Resources