I'm currently having an issue with this simple script that basically just copies and pastes input data.
The function inputting the value to the Sales Log tab but not the Pay Log tab for some reason. I'm not well versed in any form of coding so help is greatly appreciated!
function submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Food Sales");
var datasheet = ss.getSheetByName("Pay Log");
var datasheet = ss.getSheetByName("Sales Log");
var values = [[formSS.getRange("E10").getValue(),
formSS.getRange("E11").getValue(),
formSS.getRange("E12").getValue()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 3).setValues(values);
}
This is because you are using the same variable dataSheet for two different sheets.
On the line var datasheet = ss.getSheetByName("Pay Log"); you assigned the Pay Log sheet on the datasheet but then replaced it by the Sales Log sheet on the next line var datasheet = ss.getSheetByName("Sales Log");
What you can do is create and assign different variables for both sheets before pasting. Simple way you can do it is by the code below.
function submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Food Sales");
var datasheet1 = ss.getSheetByName("Pay Log");
var datasheet2 = ss.getSheetByName("Sales Log");
var values = [[formSS.getRange("E10").getValue(),
formSS.getRange("E11").getValue(),
formSS.getRange("E12").getValue()]];
datasheet1.getRange(datasheet1.getLastRow()+1, 1, 1, 3).setValues(values);
datasheet2.getRange(datasheet2.getLastRow()+1, 1, 1, 3).setValues(values);
}
Or in case you have more sheets to paste your data into, you can use the getSheets() function which will return an array that contains all your sheets. You can then loop through the sheets starting from your second sheet. Example code below
function submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Food Sales");
var dataSheet = ss.getSheets();
var values = [[formSS.getRange("E10").getValue(),
formSS.getRange("E11").getValue(),
formSS.getRange("E12").getValue()]];
for (i = 1; i < dataSheet.length; i++){
dataSheet[i].getRange(dataSheet[i].getLastRow()+1, 1, 1, 3).setValues(values);
};
};
Refer to this link for more details on the Class Spreadsheet: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getsheets
Using getValues
function submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Food Sales");
var datasheet1 = ss.getSheetByName("Pay Log");
var datasheet2 = ss.getSheetByName("Sales Log");
var values = formSS.getRange(10, 5, 3).getValues();//getValues()
datasheet1.getRange(datasheet1.getLastRow() + 1, 1, 1, 3).setValues(values);
datasheet2.getRange(datasheet2.getLastRow() + 1, 1, 1, 3).setValues(values);
}
Related
I want to copy the range a21:ay21 to a new sheet but the formula only copies the first value (a21).
I'm stuck with it. Thanks!
function submitToDataBase() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
//Input Values
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 50).setValues(values);
}
I think that in your script, values is a 4-dimensional array. And, a21:ay21 has 51 columns. If you want to use getValues and setValues, how about the following modification?
From:
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 50).setValues(values);
To:
var values = formSS.getRange("form!a21:ay21").getValues(); // formSS.getRange("a21:ay21").getValues();
datasheet.getRange(datasheet.getLastRow() + 1, 1, 1, 51).setValues(values);
As another method, I think that in your situation, you can use appendRow as follows.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
var values = formSS.getRange("form!a21:ay21").getValues(); // formSS.getRange("a21:ay21").getValues();
datasheet.appendRow(values[0]);
As another method, I think that in your situation, you can use copyTo of Class Range as follows.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("form"); //Form Sheet
var datasheet = ss.getSheetByName("Data_Base"); //Data Sheet
formSS.getRange("a21:ay21").copyTo(datasheet.getRange(datasheet.getLastRow() + 1, 1), { contentsOnly: true });
References:
getValues()
setValues(values)
appendRow(rowContents)
copyTo(destination, options)
Is this a named range?
var values = [[formSS.getRange("form!a21:ay21").getValues()]];
I would try deleting form from "form!a21:ay21", or simply using the named range writing the following in one of two ways
var values = [[formSS.getRange("form").getValues()]];
var values = [[formSS.getRange("!a21:ay21").getValues()]];
I have a problem in updating the ranges in multiple slicers at once, which is not possible.
I tried using the script below that i found in another thread for automatically deleting and recreating the slicers with the desired range.
but i get the following error. "destinationSheet" not defined.
Would appreciate in you can help me with a solution here.
Thank you.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var rowValue = sheet.getLastRow();
var newRange = sheet.getRange('A3:X'+rowValue); //new range
for (i=0; i<slicers.length; i++){
slicers[i].remove();
}
//Slicers to column A and D
destinationSheet.insertSlicer(newRange, 2, 1); // choose anchorRowPos and anchorColPos
destinationSheet.insertSlicer(newRange, 2, 4);
//Get new slicers
var slicers = destinationSheet.getSlicers();
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['']).build();
slicers[0].setColumnFilterCriteria(1, filterCriteria);
slicers[0].setTitle('Title1');
var filterCriteria2 = SpreadsheetApp.newFilterCriteria().setHiddenValues(['1']).build();
slicers[1].setColumnFilterCriteria(24, filterCriteria2);
slicers[1].setTitle('Title2');
}
Thanks to Tanaike's suggestion to change the "destinationSheet" part of the code to 'sheet', the code writen as below worked.
function test() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Tracking');
var slicers = sheet.getSlicers();
var rowValue = sheet.getLastRow();
var newRange = sheet.getRange('A3:X'); //new range
for (i=0; i<slicers.length; i++){
slicers[i].remove();
}
//Slicers to column A and D
sheet.insertSlicer(newRange, 2, 1); // choose anchorRowPos and anchorColPos
sheet.insertSlicer(newRange, 2, 4);
//Get new slicers
var slicers = sheet.getSlicers();
var filterCriteria = SpreadsheetApp.newFilterCriteria().setHiddenValues(['']).build();
slicers[0].setColumnFilterCriteria(1, filterCriteria);
slicers[0].setTitle('Title1');
var filterCriteria2 = SpreadsheetApp.newFilterCriteria().setHiddenValues(['1']).build();
slicers[1].setColumnFilterCriteria(24, filterCriteria2);
slicers[1].setTitle('Title2');
}
I am trying to extract the values from each column of the currently selected row in google sheets but my code seems to always select the values from the first row of the spreadsheet.
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var active_range = sheet.getActiveRange();
var address1 = sheet.getRange(active_range.getRowIndex(),2).getValue()
var address2 = sheet.getRange(active_range.getRowIndex(),3).getValue()
var city = sheet.getRange(active_range.getRowIndex(),4).getValue()
var state = sheet.getRange(active_range.getRowIndex(),5).getValue()
function onSelectionChange(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = e.range;
var row = range.getRow();
var val = sheet.getRange(row, 1, 1, 4).getValues();
sheet.getRange(1, 1, 1, 4).setValues(val);
}
in Google Sheets I have to repeat a function because getSheetByName() does not accept an array of sheets, it only accepts one sheet.
Is there a way to have one function that loops through specified sheets (not all sheets)?
i.e.
("Sheet1", "Sheet2" ) etc.
function recordHistory_1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
function recordHistory_2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
Reason I'm asking is because I have over 20 sheets, and so I have to write this function 20 times...
You can generalize / reuse the same function for 20 different calls as follows.
var sheetListArray = ["Sheet1", "Sheet2", "Sheet3"......"Sheet20"]
var ss = SpreadsheetApp.getActiveSpreadsheet();
for( var i = 1 ; i <= sheetListArray.length ; i++)
// You can call the below function 20 time with different Sheet name each time.
recordHistory(sheetListArray[i]);
}
function recordHistory(sheetName) {
var sheet = ss.getSheetByName(sheetName);
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
}
You can also call the function once and loop through an array of sheetnames inside the function.
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
["Sheet1", "Sheet2", "Sheet3"].forEach(function (s) {
var sheet = ss.getSheetByName(s);
var values = sheet.getRange('A2:B2').getValues()
values[0][0] = new Date();
sheet.appendRow(values[0]);
})
}
I have had some help with the last line of this code and it works perfectly for a single sheet in the spreadsheet, however I have 5 sheets in the From spreadsheet and identical 5 sheets in the target spreadsheet how can I get the same range in the other 4 sheets to their same named counterpart in the target sheet? So I used below to try and copy each sheet to its corresponding namesake but I have set up the code as below but only importing the first sheet, also is there a way to only need to enter the originating spreadsheet id once for the whole code instead of at each sheet section?
function ImportDataRange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('AM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('AM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('PM trip'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('PM trip'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Pool / Beach'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('Pool / Beach'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Night Dive'); //To Sheet Name
var ssraw = SpreadsheetApp.openById('1Z0zU2oGktLPB-QitYMm9XtOGN4Rd-Z-UJJomGX4NIO0'); // From Spreadsheet ID
var sheetraw = ssraw.getSheetByName('Night Dive'); // From Sheet name
var range = sheetraw.getRange('B7:U38');
var data = range.getValues();
var lastRow = sheet.getLastRow()+1;
sheet.getRange(lastRow,3,data.length,data[0].length).setValues(data)
}
Thank you for any help. Much appreciated.
The code can be simplified using a loop.
You said above that you have 5 sheets but you only have 4 in the code sample, add the other sheet name to var sheetNames.
function onOpen() { // This function adds a custom menu to the spreadsheet (Backup to archive) so you can run the script from there.
var ui = SpreadsheetApp.getUi();
ui.createMenu('Backup to archive')
.addItem('Backup', 'dataBackup')
.addToUi();
}
function dataBackup() {
var inputSS = SpreadsheetApp.openById('1wglM4-5jx873vwtFRPVgC1qk27JjrsYjDwp0fNpl5Xg'); // The file ID of the Input sheet
var archiveSS = SpreadsheetApp.openById('146WU8RghfFqlCpCSX7n6kBAKOyxcpVKt14yhVfvYz-g'); // The file ID of the archive sheet
// The names of the sheets to be copied.
// NOTE: These names must match the names of the sheets in BOTH spreadsheets i.e. the same case the spelling and the same spaces.
var sheetNames = ['AM trip', 'PM trip', 'Pool / Beach', 'Night Dive'];
for (var i = 0; i < sheetNames.length; i++) { // Loop to each sheet listed in 'var sheetNames' and copy the data.
var inputSheet = inputSS.getSheetByName(sheetNames[i]);
var archiveSheet = archiveSS.getSheetByName(sheetNames[i]);
var data = inputSheet.getRange('B7:U38').getValues();
archiveSheet.getRange(archiveSheet.getLastRow() + 1, 2, data.length, data[0].length).setValues(data);
}
}