AppendRow in a specific range - javascript

I have a code to add a array of content in a row but i need to add it in a specific range so the checkboxes in the first column are not read as information, so it can add up the row but in the same row that the checkbox it's in.
function criarRegistro() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("EQUIPAMENTOS NO LABORATÓRIO");
var destinyLog = ss.getSheetByName("EQUIPAMENTOS NO LABORATÓRIO");
sheet.activate();
var source = sheet.getRange("C2:G2");
var rangeLimpeza = sheet.getRange("C2:G2");
var values = source.getValues();
//rangeLimpeza.clearContent();
destinyLog.appendRow(values[0]);
ss.toast("Registro finalizado");
SpreadsheetApp.flush();
}
That's the code i'm using, basically i want to appendRow or copy the row information starting in B column, while in the A column i would have a preset of unmarked checkboxes.

You cannot use appendRow but you can calculate the correct destination range where the values should be copied and then use a basic getRange().setValues().
Rewriting and cleaning your function
function criarRegistro() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("EQUIPAMENTOS NO LABORATÓRIO");
var destinyLog = ss.getSheetByName("EQUIPAMENTOS NO LABORATÓRIO");
var source = sheet.getRange("C2:G2");
var values = source.getValues();
var nbOfCols = values[0].length; // how many columns do we need
var lastLineOfData = destinyLog.getDataRange().getNumRows() // how many rows are already present in the destination
destinyLog.getRange(lastLineOfData+1, 2, 1, nbOfCols).setValues(values) // we write after the last line, starting at the second column (B)
ss.toast("Registro finalizado");
SpreadsheetApp.flush();
}

Related

Google Apps Script - If Last Column in table, then paste data with new row

My information is set in columns (A to E), info paste in 2 rows (A2:A3, B2:B3...). What I want to happen is when info set in last column E (E2:E3, E5:E6 and more), new info paste with a new row in column A (A5:A6, B5:B6).
My info is in the spreadsheet:
This code paste info doesn't work correctly:
function submitData1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Job"); //Data Sheet
//Input Values
var values1 = [[formSS.getRange("B6").getValue()], [formSS.getRange("B7").getValue()]];
var sr = 1
var sc = 1
var nr = 2
var nc = 1
for (var i=1;i<1;i++)
var workingCell = datasheet.getRange(i,7);
{
if (workingCell ==null)
{datasheet.getRange(sr,datasheet.getLastColumn()+1,nr,nc).setValues(values1)
}
else {datasheet.getRange(datasheet.getLastRow()+1, sc, nr, nc).setValues(values1)
sc= sc+1}
}
}
Here is a sample replication code:
function submitData1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var datasheet = ss.getSheetByName("Job"); //Data Sheet
//Input Values
var values1 = [["Name X"], ["Job X"]];
var lastRow = datasheet.getLastRow();
var lastRow_ColCount = datasheet.getRange(lastRow,1,1,5).getValues().flat().filter(String).length;
Logger.log(lastRow);
Logger.log(lastRow_ColCount);
if(lastRow_ColCount==5){
//Add data on a new row, column A
datasheet.getRange(lastRow+2, 1,2,1).setValues(values1);
}
else{
//Add data on the next column in the same row
datasheet.getRange(lastRow-1, lastRow_ColCount+1,2,1).setValues(values1);
}
}
I used a fixed value for the input data during this replication.
Just replace the values from your original code that will be read from your form sheet
You can also remove the logs in the script, I just used them to debug the code
What it does?
Get the last row of the sheet, It will determine the last row that has a valid data
Get the data/values of the last row from column A to column E. Use Array.flat() to change 2-d array to 1-d array. Use Array.filter() to get non empty cells then count the data available in the last row.
If the current data available is 5, then the new data will be added on the new row, else the new data will be added on the next column
Output:

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()

How to create a note on a Spreadsheet cell based on the value of the selected cell

To get this specific function to work, I'm trying it out in a simple test sheet.
I've got two sheets(STATUS and FEBRUARI) in the FEBRUARI sheet I've selected a certain cell. This cell has a value. What I want the script to do is to look at that value, find that value in the STATUS sheet(say it finds it in A1) and return the value in B1 to a cell note in the selected cell in the FEBRUARI sheet. As example: in the cell it says "Project 6" and the cell-note gives info about this project.
This is what I got. This gives me a certain value(-1) but it doesn't seem to matter where i put the lookupvalue.. it always returns -1.
// My Script
function noteSetter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lookupvalue = SpreadsheetApp.getActiveSheet().getActiveCell().getValue();
var sheet = ss.getSheetByName("STATUS"); //source sheet
var sheet2 = ss.getSheetByName("FEBRUARI"); //result sheet
var cellnote = SpreadsheetApp.getActiveSheet().getActiveCell();
var lc = sheet.getLastColumn()
var lookup = sheet.getRange(1,1,1,lc).getValues() //
var index = lookup.indexOf(lookupvalue)
cellnote.setNote(index);
// This part will actually run the script once it's up and running
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Set cell note",
functionName : "noteSetter"
}];
sheet.addMenu("Scripts", entries);
};
}
var lookup = sheet.getRange(1,1,1,lc).getValues();
var index = lookup.indexOf(lookupvalue)
The first line returns a 2D array. indexOf() only works on flattened arrays. Try using:
var lookup = sheet.getRange(1,1,1,lc).getValues()[0];
According to Google preferably use getCurrentCell() instead of getActiveCell() because it returns the current highlighted (or selected) cell.
Also your onOpen() function should be outside of your noteSetter() function otherwise it is not being called when the spreadsheet opens.
The following code will do what you want for a sheet like yours. If the data order is altered you have to alter the range formulas accordingly.
/*
* This function will run when the Spreadsheet is opened and,
* will add a Menu item for the noteSetter function
*/
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Set cell note",
functionName : "noteSetter"
}];
sheet.addMenu("My Menu", entries);
};
function noteSetter() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("STATUS"); //source sheet
var sheet2 = ss.getSheetByName("FEBRUARI"); //result sheet
var noteCell = sheet2.getCurrentCell();
var lookUpValue = noteCell.getValue();
// Search through Col C in "STATUS" sheet to get the matching row
// You need to transpose the lookUpRange
var lookUpRange = sheet.getRange(2,3,sheet.getDataRange().getLastRow(),1).getValues();
lookUpRange = transpose(lookUpRange);
var index = lookUpRange[0].indexOf(lookUpValue); // Starts at 0
var row = index + 2; // One for the omitted header and one because rows start at 1
var note = sheet.getRange(row,7).getValue();
noteCell.setNote(note);
}
// You need to transpose to avoid looping through the array
function transpose(a)
{
return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}

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)

copy google form response rows into new sheet

Any help is greatly appreciated.
I am trying to get form responses to move over to another sheet if cell in row 5 is greater than 0. The code I am using below moves it over but adds it under the last cell that has a formula in it. Is there a modified version of this code that i can tell it to ignore formula's in cell. Also only copy over rows A:E. Thanks
function moveToAppropriateSheet(e) {
// assumes source data in sheet named Form Responses 1
// target sheet of move to named TR Requested
// test column with yes/no is col 11 or K
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Query');
var r = s.getRange(s.getLastRow(),1,1,20);
var data = r.getValues();
if(data[0][4] >0) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Pending");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
targetSheet.getRange(targetSheet.getLastRow()+1,1,1,20).setValues(data);
s.deleteRow(row);
}
};
Edited in light of new information:
You want to copy only the first 5 columns of the row you copy in "Query" to the sheet "Pending".
You already have entries in "Pending", in columns F, G, H, ... You wish to add your copied row under the last entry in columns A:E
I've also assumed for simplicity's sake that you always copy 5 columns so I can check only column A to find the last row (in the subset A:E).
I also note that this is essentially a duplicate of this answer by Mogsdad.
If my assumptions are correct, then:
function moveToAppropriateSheet(e) {
// assumes source data in sheet named Form Responses 1
// target sheet of move to named TR Requested
// test column with yes/no is col 11 or K
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Query');
var r = s.getRange(s.getLastRow(),1,1,5); // Change 20 to 5 to copy only cols A:E
var data = r.getValues();
var targetSheet = ss.getSheetByName("Pending");
var lastRowIgnoringFormulae = 0;
var targetSpace = targetSheet.getRange("A:A").getValues();
var lastRow = targetSpace.filter(String).length;
if(data[0][4] >0) {
var row = r.getRow();
var numColumns = s.getLastColumn();
targetSheet.getRange(lastRow+1,1,1,5).setValues(data); // +1 to convert into R1C1 notation
s.deleteRow(row);
}
};
I hope I've helped.

Categories

Resources