I have a function that copies and pastes info from one sheet to two others, then sorts all three sheets differently. The first sheet is a simple alpha sort by column 1, the second sheet is an alpha sort by column 2, and the third sheet sorts by columns 3-8 in that order. When data is edited on "Student list details" the user clicks the "SORT" button to run this script and modify all three pages. However, the third page (AutoDocHeidiOnly2) is not sorting properly. There is no error given, it appears to run fine, but the sheet is not sorted.
function myFunction() {
//var copyFromRange = 'Student List Details!A3:H1000';
//var copyToRangeStart = 'AutoDocHeidiOnly!A3:H1000';
// copyValuesOnly(copyFromRange, copyToRangeStart);
var sld = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student List Details");
var ado = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AutoDocHeidiOnly");
var ado2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AutoDocHeidiOnly2");
var sourceRange=sld.getRange("A3:H1000");
var sourceData = sourceRange.getValues();
ado.getRange(3,1,sourceData.length,sourceData[0].length).setValues(sourceData);
ado2.getRange(3,1,sourceData.length,sourceData[0].length).setValues(sourceData);
var lastRow = sld.getLastRow()
var range = sld.getRange(3,1,lastRow,9);
range.sort(1);
var lastRow = ado.getLastRow()
var range = ado.getRange(3,1,lastRow,8);
range.sort(2);
var lastRow = ado2.getLastRow()
var range = ado2.getRange(3,1,lastRow,8);
range.sort({Column: 3},{Column: 4},{Column: 5},{Column: 6},{Column: 7},{Column: 8});
// ado.getDataRange()
// var rowssld = sld.getDataRange().getValues();
// var ado2 = sheet.getSheets('AutoDocHeidiOnly2')
//var row = sld.getRange(88, 3, 1, 9).getValues();
// ado.appendRow(row[0]);
// var targetrange = ado.getRange(3, 1, sld.getLastRow(), 9);
//var rangeValues = sld.getRange(3, 1, sld.getLastRow(), 9).getValues();
//targetrange.setValues(rangeValues);
//for (var i = 3; i < rowssld.length; i++){
// for (var j = 1; j < 9; j++){
// var val=sld.getRange(i,j).getValue();
// ado.getRange(i,j).setValue(val);
// }
// }
Browser.msgBox("FINISHED") ;
}
function test()
{
var sld = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student List Details");
var ado = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AutoDocHeidiOnly");
var ado2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("AutoDocHeidiOnly2");
var sourceRange=sld.getRange("A3:H1000");
var sourceData = sourceRange.getValues();
ado.getRange(3,1,sourceData.length,sourceData[0].length).setValues(sourceData);
ado2.getRange(3,1,sourceData.length,sourceData[0].length).setValues(sourceData);
var lastRow = sld.getLastRow()
var range = sld.getRange(3,1,lastRow,9);
range.sort(1);
var lastRow = ado.getLastRow()
var range = ado.getRange(3,1,lastRow,8);
range.sort(2);
var lastRow = ado2.getLastRow()
var range = ado2.getRange(3,1,lastRow,8);
range.sort(3);
// ado.getDataRange()
// var rowssld = sld.getDataRange().getValues();
// var ado2 = sheet.getSheets('AutoDocHeidiOnly2')
//var row = sld.getRange(88, 3, 1, 9).getValues();
// ado.appendRow(row[0]);
// var targetrange = ado.getRange(3, 1, sld.getLastRow(), 9);
//var rangeValues = sld.getRange(3, 1, sld.getLastRow(), 9).getValues();
//targetrange.setValues(rangeValues);
//for (var i = 3; i < rowssld.length; i++){
// for (var j = 1; j < 9; j++){
// var val=sld.getRange(i,j).getValue();
// ado.getRange(i,j).setValue(val);
// }
// }
Browser.msgBox("FINISHED") ;
}
//function copyValuesOnly(copyFromRange, copyToRangeStart) {
// var ss = SpreadsheetApp.getActiveSpreadsheet();
//var source = ss.getRange(copyFromRange);
//source.copyTo(ss.getRange(copyToRangeStart), {contentsOnly: true});
Just taking a crack at this, but did you try specifying the ascending order for the columns on the 3rd sheet? For example:
range.sort({Column: 3},{Column: 4},{Column: 5},{Column: 6},{Column: 7},{Column: 8});
Try:
range.sort([{Column: 3, ascending: true},{Column: 4, ascending: true},{Column: 5, ascending: true},{Column: 6, ascending: true},{Column: 7, ascending: true},{Column: 8, ascending: true}]);
By no means am I one of the wizards on here, but I've ran into some funky issues myself, so just trying to be helpful! :)
Related
Im trying to get the url of a editable google form response to show up in google sheets,but it does not seem to be working.
I have seen Awesome Table and Ruben's example. Based of these 2 links and some others, they seem to be working for single form response sheets,but not multiple.
I tried this code 1st:
var formURL = 'https://docs.google.com/forms/d/__Your ID__/viewform';
var sheetName = '__Response sheet__';
var columnIndex = __column where it appears__;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}
2nd is:
// Form URL
var formID = '__Your ID__';
// Sheet name used as destination of the form responses
var sheetName = '__Response sheet__'';
/*
* Name of the column to be used to hold the response edit URLs
* It should match exactly the header of the related column,
* otherwise it will do nothing.
*/
var columnName = '__name of column where it appears__' ;
// Responses starting row
var startRow = 2;
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(formId);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] && !data[i][columnIndex]) {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Nothing is showing up, and when i check the logs for triggers, it is all working fine, no failures.I have tried putting the global variables within the function, but no changes.
The following points are taken from the latest version from #Rubén latest version on github. This code is a thing of beauty and a joy to behold. Combined with Rubén's detailed instructions, this answer can be setup and running in less than 5 minutes.
change
var formID = '__Your ID__';
to
var formURL = 'https://docs.google.com/forms/d/ -insert id - /edit';
you get the URL from the Forms Editor page.
change
var columnName = 'Form URL';
to
var sheetName = 'URL'; // Column U
replace getEditResponseUrls entirely
function getEditResponseUrls(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var columnIndex = headers[0].indexOf(columnName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openByUrl(formURL);
for(var i = startRow-1; i < data.length; i++) {
if(data[i][0] != '' && data[i][columnIndex] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if(formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl);
}
}
}
Remember to set the installable trigger.
In the Google Sheet that you have linked from the question above, you have declared two functions with the same name and this could be a reason why the first one may never be executing. I tested this code and it seemed to work.
You may want to replace everything in the file with just this code.
var id = '1SdSPhOwi1dWQzRsNpA9zFL-ODorgohST3TMRJLqz16I';
var sheetName = 'Order Information';
var columnIndex = 21;
function getEditResponseUrls() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var data = sheet.getDataRange().getValues();
var form = FormApp.openById(id);
for(var i = 2; i < data.length; i++) {
if (data[i][0] != '' && data[i][columnIndex-1] == '') {
var timestamp = data[i][0];
var formSubmitted = form.getResponses(timestamp);
if (formSubmitted.length < 1) continue;
var editResponseUrl = formSubmitted[0].getEditResponseUrl();
sheet.getRange(i+1, columnIndex).setValue(editResponseUrl);
}
}
}
I am attempting to create a additional menu option that allow me to automatically delete all rows that contain the text "Complete" in Column D. The script I have runs and finishes with no errors but does not delete the rows. I tried to find help on line and tweaked the code but same result
function readRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 'Complete';
for (var i = 'Complete'; i <= numRows - 1; i++) {
var row = values[i];
if (row[2] == 'Complete' || row[2] == '') {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
};
function onOpen() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var entries = [{
name : "Delete Rows where column shows Complete Text",
functionName : "readRows"
}];
sheet.addMenu("Script Center Menu", entries);
};
I'm new to coding. I am attempting to import e-mail addresses from a Google Sheet to my gmail account. Any help on why this code is not working would be greatly appreciated! Here is the code I have entered:
function myFunction() {
var alreadyAdded = "Already added";
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 396; // Number of rows to process
// Fetch the range of cells A2:F397
var dataRange = sheet.getRange(startRow, 1, numRows, 400
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for
var
for (var i = 0; i < data.length; ++i)
};
var row = data[i];
var lastName = row[0]
var firstName = row[1]
var emailAddress = row[2]
var address = row[3]
var notes = row[4]
if (addedAlready != alreadyAdded) {
// Create contact in Google Contacts
var contact = ContactsApp.createContact(firstName, lastName, emailAddress);
// Add values to new contact
contact.addAddress(address, "");
contact.setNotes(notes);
sheet.getRange(startRow + i, 7).setValue(alreadyAdded);
};
};
}
try it var dataRange = sheet.getRange(startRow, 1, numRows, 400);
Please see the previous post on this question;
Auto Sort not working on Multiple Sheets within one Google Sheet
Here's the good script from that last session;
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var tableRange = "A3:I99"; //range to be sorted
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}
else{return}
}}
function test_onEdit() {
onEdit({
user : Session.getActiveUser().getEmail(),
source : SpreadsheetApp.getActiveSpreadsheet(),
range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
authMode : "LIMITED"
});
}
This thread successfully resolved my question, however, now what I want to do is to Auto Sort a 3rd Sheet, which is locked to others, but not to myself. The 3rd Sheet will be titled "Karlan Production for 2017". I also want this 3rd sheet to pull all new data from the 2nd Sheet "Deals in Escrow" and in the same formatting as the 2nd Sheet and to auto sort as the data is imported to the 3rd Sheet, but also be able for me to input new deals manually as well in the 3rd Sheet and still be able to auto update.
I believe this will do what you want:
function onEdit(event){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = event.source.getActiveSheet().getName()
var editedCell = event.range.getSheet().getActiveCell();
if(sheet=="Loan Inquiries"){
var columnToSortBy = 2;
var tableRange = "A3:G99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
}}
else if(sheet=="Deals in Escrow"){
var columnToSortBy = 7;
var tableRange = "A3:I99"; //range to be sorted
if(editedCell.getColumn() == columnToSortBy){
var range = ss.getActiveSheet().getRange(tableRange);
range.sort( { column : columnToSortBy, ascending: true } );
combineData()
}}
else if(sheet=="Karlan Production for 2017"){
var columnToSortBy = 7;
if(editedCell.getColumn() == columnToSortBy){
combineData()
}}
else{return}
}
function combineData(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s=ss.getSheets()[1]
var lr = s.getLastRow()
var s1=ss.getSheets()[2]
var lr1 = s1.getLastRow()
var lc1=s1.getLastColumn() //get the last column of 'Karlan Production for 2017'
var rng=s.getRange(3, 1, lr,lc1).getValues() //get 'Deals in Escrow' values
var rng1=s1.getRange(3, 1, lr1,lc1).getValues() //get 'Karlan Production for 2017' values
var rng2=[]
var rng2=rng.concat(rng1) //combine 'Deals in Escrow' and 'Karlan Production for 2017' values (all rows)
removeDuplicates(rng2) // call remove duplicates sending combined array
}
function removeDuplicates(data) {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var s1=ss.getSheets()[2]
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);//create array of non duplicate rows
}
}
s1.clearContents(); //clear 'Karlan Production for 2017'
var sor=s1.getRange(3, 1, newData.length, newData[0].length).setValues(newData); //set new array
sor.sort( { column : 7, ascending: true } );//sort new data
}
After adding the new sheet, click the arrow on the sheet tab.
Click Protect Sheet. In the popup, enter a description and click
the Sheet button. Click the Set permissions button. In the Range
editing permissions popup click the arrow by 'Only you'. Select
'Custom' and add the email address of the other person you want
to be able to edit the sheet. Click done.
I am running a script in google sheets that uses an API from Wunderground to gather my local weather. I am having issue now when I try to use highcharts as it doesn't like the % sign that comes from the API json. My code is below and I'm trying to figure out how to modify it
<Code>
/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
var cDay = 0, cTemp = 1, cRH = 2, cWinddegree = 3;
var cWindspeed = 4, cWindgust = 5;
var nCols = 7;
function getTemp() {
var url = 'http://api.wunderground.com/api/***************/conditions/q/44.1,-77.3.json';
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var response = UrlFetchApp.fetch(url);
var contentText = response.getContentText();
var conditions = JSON.parse(contentText);
var todaysConditions = conditions;
var temp = todaysConditions.current_observation.temp_f;
var rh = todaysConditions.current_observation.relative_humidity;
var winddir = todaysConditions.current_observation.wind_dir;
var winddegree = todaysConditions.current_observation.wind_degrees;
var windspeed = todaysConditions.current_observation.wind_mph;
var windgust = todaysConditions.current_observation.wind_gust_mph;
sheet.insertRowAfter(1);
var range = sheet.getRange(2,1,1, nCols);
var row = range.getValues()[0];
row[cDay] = new Date ();
row[cTemp] = temp;
row[cRH] = rh;
row[cWinddegree] = winddegree;
row[cWindspeed] = windspeed;
row[cWindgust] = windgust;
range.setValues([row]);
}
</Code>
I believe it is this line I need to modify:
row[cRH] = rh;