Ive been working on automatically sorting my data (ascending based on 2nd row 1st column data) and I found some tips through searching online but encountered an error which seems I cant find an answer through the net.
so heres the scenario:
I have 2 sheets, Sheet1 & Sheet2, the data from sheet1 is linked through sheet2 although sheet2 has additional columns,
this is sheet1
and this is sheet2
notice that the Column lastname and code in both sheets are thesame, the difference is the column Gender (Formatted on drop-down list) & Bdate(cell formatted as date)
I found a script that seems to work but I does not properly work completely, here is the output after I run the script.
notice the columns that inside the red box, it seems gender and bdate didnt follow the auto sort.
here is my code:
function autosortOnEdit() {
var sheetNames = ["Sheet1", "Sheet2"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetNames.forEach(function(name) {
var sheet = ss.getSheetByName(name);
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn() -1);
range.sort({column: 1, ascending: true});
});
}
my observation is I think this script does not work on cells that are formatted like the example above.
I want to sort this automatically based on column A "last name".
how can i make this script work even on formatted cells?
Thanks in Advance, I will continue searching through the net.
Not sure how to use range.sort({column: 1, ascending: true}); or how does it work, but whenever I want to sort sheet values, I do the following:
function myFunction()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var rows = sheet.getLastRow();
var columns = sheet.getLastColumn();
var sortedValues = sheet.getRange(2, 1, rows-1, columns).getValues().sort();
sheet.getRange(2, 1, rows-1, columns).setValues(sortedValues);
}
Try this instead of your code, hope this helps as it is successfully sorting all the values when I tested.
EDIT
To apply the same to all sheets inside a spreadsheet, you can get sheet names and iterate it in for loop one by one. Check the code:
function myFunction()
{
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var currentSheetName, currentSheet;
for(var i=0; i<sheets.length; i++)
{
currentSheetName = sheets[i].getName();
currentSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(currentSheetName);
var rows = currentSheet.getLastRow();
var columns = currentSheet.getLastColumn();
var sortedValues = currentSheet.getRange(2, 1, rows-1, columns).getValues().sort();
currentSheet.getRange(2, 1, rows-1, columns).setValues(sortedValues);
}
}
Related
I have a form that is filled in and i would like for the answers coming in to be formatted as follow:
Text to be Wrapped, Centred Vertically as well as horizontally within the cell. As well as for the answers to be sorted from the youngest to the oldest based on column A
I have managed to pull some scripts but cannot seem to get them working in conjunction. Please help !!
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s= ss.getActiveSheet()
var lr = s.getLastRow()
var r= s.getRange(1, 1, lr,4)
var set=r.setHorizontalAlignment("center")
var range = e.range;
range.setWrap(true);
}
function sortResponses() {
var sheet = SpreadsheetApp.getActive().getSheetByName("Form Responses 1");
sheet.sort(1, false);
}
and second
function Alignment() {
const spreadsheet = SpreadsheetApp.getActive();
const sheets = spreadsheet.getSheets();
sheets.forEach(sheet=>{
let rg=sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
rg.setHorizontalAlignment('center')
.setVerticalAlignment('middle');
});
};
Please help
Here is a little script snippet I use for doing a similar thing.. Its not my best code but does the job.
The following function is set to run with a On Form Submit trigger,
function sort_and_format_FormResponses() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses 1');
//Sort Responses by Date Stamp
var range = sheet.getRange("A2:G");
range.sort({column: 1, ascending: false})
range.setWrap(true);
//Column Specific Formatting...
//Format column D to Plain text
range = sheet.getRange("D:D");
range.setNumberFormat('#STRING#');
range.setHorizontalAlignment('left');
}
function sort_and_format_FormResponses() {
var sh = SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
var rg = sh.getRange("A2:G" + sh.getLastRow());//Don't use unterminated ranges like A2:G in apps script because in generates a lot of nulls for lastrow to maxrows
rg.sort({column: 1, ascending: false})
rg.setWrap(true);
rg = sh.getRange("D1:D" + sh.getLastRow());
rg.setNumberFormat('#STRING#');
rg.setHorizontalAlignment('left');
}
Using Google Apps Script for Google Sheet, this formula works to copy and paste visible row 77 :
function copyRow77() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lRow = sheet.getLastRow();
var lCol = sheet.getLastColumn();
var range = sheet.getRange(77,1,1, lCol);
sh.insertRowsAfter(lRow, 1);
range.copyTo(sh.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}
}
But when row 77 is filtered, it doesn't work anymore...
Maybe an idea ?
As AsyntuBU stated:
You can use getFormulasR1C1, that will adapt to other rows/columns when copied. You can start at that. This is in no way an answer to your question, but to your comment where getFormulas wont adapt to new rows/columns
I have a Google sheet with 9 columns. B column is the actual spreadsheet name where they should be. I tried to read data from "All" spreadsheet and send them to the last row of the spreadsheet named in the second row but I'm getting "Exception: Bad value" messages. The code is stated below :
var ui = SpreadsheetApp.getUi();
function onOpen() {
ui.createMenu('Send')
.addItem('Send', 'send')
.addToUi();
};
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(1,1, lastRow, 9);
function send() {
var rangeValues = searchRange.getValues();
for ( i = 1 ; i < lastRow; i++){
var targetSheet = ss.getSheetByName(rangeValues[i][1]);
targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, 9).setValues(rangeValues[i][9]);
}
};
Could anyone suggest how can I do this?
In your script, the values are retrieved from 9 columns from the column "A" to "I". But at targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, 9).setValues(rangeValues[i][9]);, you are trying to retrieve the value from the column "J". And, values of setValues(values) is required to be 2 dimensional array. I think that there might be the reason of your issue in those. For this, I would like to propose the following 2 patterns.
Pattern 1:
If you want to put the value of each row to each sheet, how about the following modification?
From:
targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, 9).setValues(rangeValues[i][9]);
To:
targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, 9).setValues([rangeValues[i]]);
Pattern 2:
If you want to put the value of last column (the column "I") to the next row of last row to each sheet, how about the following modification?
From:
targetSheet.getRange(targetSheet.getLastRow()+1, 1, 1, 9).setValues(rangeValues[i][9]);
To:
targetSheet.getRange(targetSheet.getLastRow()+1, 1).setValue(rangeValues[i][8]);
References:
setValues(values)
setValue(value)
I'm trying to automate a sheet to sort rows based on date, and transfer rows when a checkbox is selected. I got the transfer part figured out, but the sort function only works on the tabs I'm getting rid of, and not the tabs I'm keeping, and I don't understand why.
Here's a couple sort functions that both work, but not on the tabs I need them to work on:
function onEdit(event) {
// assumes source data in sheet named Active
// target sheet of move to named Completed
// getColumn with check-boxes is currently set to colu 8 or H
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
//sort Active by oldest date at the top, using column D or 4
var sheet = event.source.getActiveSheet();
var editedCell = sheet.getActiveCell();
var columnToSortBy = 4;
var tableRange = "A2:T99"; // What to sort.
if(editedCell.getColumn() == columnToSortBy){
var range = sheet.getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
}
And:
function myFunction()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Active");
var rows = sheet.getLastRow();
var columns = sheet.getLastColumn();
var sortedValues = sheet.getRange(2, 1, rows-1, columns).getValues().sort();
sheet.getRange(2, 1, rows-1, columns).setValues(sortedValues);
}
The sort works on the tabs with names on them, but not the tabs called Active and Completed. With the automation, I'm getting rid of the named tabs and we'll just have Active and Complete.
I've removed the transfer upon complete function as it worked just fine, and I'm just focusing on the sort, since it seems to be the issue.
Any suggestions for why the two tabs aren't sorting?
Here's a copy of the spreadsheet I'm working on:
https://docs.google.com/spreadsheets/d/17VeCzfqAclxUN-kVIrWZSII-7clsNfttmVVU8s0JhFs/edit?usp=sharing
EDIT: solved by Cooper below.
Final version of code that worked for my needs:
function onEdit(event) {
var sh=SpreadsheetApp.getActive().getSheetByName("Active");
sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).sort({column:4,ascending:true});
}
Try this for sorting by column 4:
function myFunction() {
var sh=SpreadsheetApp.getActive().getSheetByName("Active");
sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn()).sort({column:4,ascending;true});
}
Sorting
I want to take the last response from a Google Form and copy it to the same range on another sheet.
The second sheet has some data in columns A & B (a login and password) that I want to assign as somebody completes the registration form.
I want the data from column B to H from the form response sheet to be copied to column C to I on the sheet that contains the login/password columns when a response is received.
I know I am missing the code to take the range from the last row, but I know NO coding at all and my current script is compiled from things I've found around the web and the reference material.
Any advice would be greatly appreciated.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("sheetid");
var sheet = spreadsheet.getSheets()[0];
var lastrow = sheet.getLastRow();
var range = sheet.getRange(2,2,1,7);
values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var ssRange = SSsheet.getRange(2,3,1,7);
ssRange.setValues(values);
}
Try using copyTo(destination). It copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
// The code below will copy the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));
}
You can also try using copyValuesToRangecopyValuesToRange.
For additional code samples, you may also refer to this forum.
function onEdit(e) {
var spreadsheet = SpreadsheetApp.openById("ID");
var sheet = spreadsheet.getSheets()[0];
var lastRow = sheet.getLastRow();
var range = sheet.getRange("A"+(lastRow)+":G"+(lastRow));
var values = range.getValues();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSsheet = ss.getSheets()[1];
var getRow = SSsheet.getLastRow() + 1;
var ssRange = SSsheet.getRange(getRow, 1, values.length, values[0].length);
ssRange.setValues(values);
Try with this,
this code copies the last value you want from a certain range and pastes it in the last available row of the other sheet.
The code it's the same only in the place where you are going to copy it, specify it to be the size of what I want to copy. I hope it helps you :D