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()]];
Related
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);
}
I use a function that allows me to copy-paste the data from the user form (sheet "Form") to the sheet "Data" by using the button "Submit" with the following script assigned to this button:
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
//Input Values
var values = [[formSS.getRange("A3").getValue(),
formSS.getRange("B3").getValue(),
formSS.getRange("C3").getValue(),
formSS.getRange("D3").getValue(),
formSS.getRange("E3").getValue(),
formSS.getRange("F3").getValue()]];
datasheet.getRange(datasheet.getLastRow()+1, 1, 1, 6).setValues(values);
}
So, I fill the cells from A3 to F3, then press the button and the info goes to the sheet Data. Another entering goes to the Data sheet as the next row.
Actually I copy-paste the row A3:F3 and it works good, but now I need to copy-paste several rows at one time: the range A3:F10. How should I change the script for this purpose?
Solution:
To copy and paste an entire range of values you can use getValues() and setValues().
function submitData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form"); //Form Sheet
var datasheet = ss.getSheetByName("Data"); //Data Sheet
//Input Values
var values = formSS.getRange("A3:F10").getValues();
datasheet.getRange(datasheet.getLastRow()+1, 1, 8, 6).setValues(values);
}
Sample Output:
Input:
Output:
I copy the data of Sheet1 to Sheet2 with the code below.
When I inserted an arrayformula(A2:A&B2:B) in Sheet2, the sript stopped working and i got the error:
coordinates of the target range are outside the dimensions of the
sheet array formula
When I deleted the arrayformula, the execution of the script completed successfully.
What should i change to the code (or to the arrayformula) to get them to cooperate?
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var range = sourceSheet.getDataRange();
range.copyTo(logSheet.getRange(lastRow + 2, 1), {contentsOnly: true}); // paste under previous paste
}
Issue:
The input range and the output range do not match:
Input range: range = sourceSheet.getDataRange()
Output range: logSheet.getRange(lastRow + 2, 1)
The output range is a single cell, while the input range is the full data range (therefore multiple cells).
Solution:
If the input range is dynamic it is a better idea to use getValues/setValues instead:
function dailyLog() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName('Sheet1');
var logSheet = ss.getSheetByName('Sheet2');
var lastRow = logSheet.getLastRow();
var values = sourceSheet.getDataRange().getValues();
logSheet.getRange(lastRow+1,1,values.length,values[0].length).setValues(values);
}
and now the size of the data (values) is dynamically set with the help of the length function.
Reference:
getValues()
setValues()
I need to enter data in a Google sheet cell, which cell is in a named range. To do that using a Google Script, I need to know the named range's column number. I have written a script that gets the job done, but only because I know the named range has only one column, so I use the getLastColumn command to get the column number. I know there has to be a command to "getColumnNumber" or something like that, but from Google documentation and this site, I simply can't find it. Any help? Bound script below:
function addSong() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = Browser.inputBox('New Song', 'Enter the song title', Browser.Buttons.OK_CANCEL);
var namedRange = sheet.getRange("Title");
var col = namedRange.getLastColumn();
sheet.getRange([lastRow+1], col).setValue(songTitle)
SpreadsheetApp.setActiveRange(range);
}
Hey Cooper: I tried your approach and it works great, except it loses some of what I was doing with my script; specifically, it does not increment the song record number. So, I added the key bit from what you did to get the named range column correctly into my code and it all works. Revised code is:
function addSong() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow()
sheet.appendRow([lastRow+1]);
SpreadsheetApp.flush();
var range = sheet.getRange(sheet.getLastRow(), 1);
var songTitle = Browser.inputBox('New Song', 'Enter the song title', Browser.Buttons.OK_CANCEL);
var namedRange = sheet.getRange("Title");
var range=sheet.getRange(sheet.getLastRow(), namedRange.getColumn())
range.setValue(songTitle);
SpreadsheetApp.setActiveRange(range);
}
Try this:
function addSong() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var resp=SpreadsheetApp.getUi().prompt('New Song', 'Enter the sone title', SpreadsheetApp.getUi().ButtonSet.OK_CANCEL);
var songTitle=resp.getResponseText();
var namedRange = ss.getRangeByName('Title');
var range=sh.getRange(sh.getLastRow()+1, namedRange.getColumn())
range.setValue(songTitle);
SpreadsheetApp.setActiveRange(range);
}
range.column documentation
I want to know if we can select a row in a Spreadsheet using Google script like this
Anyway first I've tried with the columns by using this fonction but I get an error of the data (is not found): If you have an idea :)
function testGetFullColumn()
{
getFullColumn('A', 1) ;
}
// This function gets the full column Range like doing 'A1:A9999' in excel
// #param {String} column The column name to get ('A', 'G', etc)
// #param {Number} startIndex The row number to start from (1, 5, 15)
// #return {Range} The "Range" object containing the full column: https://developers.google.com/apps-script/class_range
function getFullColumn(column, startIndex){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var lastRow = sheet.getLastRow();
return sheet.getRange(column+startIndex+':'+column+lastRow);
}
Edit :I know about getRange but I would be cool^^^^^^ to see in visually the selection like in the photo I've attached because I tried with
var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheets()[0]; return sheet.getDataRange();
and it doesn't select all the sheet visually I mean I want to see an interactive selection :) like here
Edit 2: I've tried to use setActiveRange after getRange as you suggested thanks Guilherme M but it doesn't select visually all the document :( Your code works it's really cool^ I was afraid that it's impossible to select I will try to do the same thanks ^^
function testGet()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getDataRange();
SpreadsheetApp.setActiveRange(range)
}
Edit 3: To select all the document I've tried this code:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var range = sheet.getRange(1,1,lastRow+1,lastColumn+1);
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: tout le document
var activeRange = selection.getActiveRange();
Yes, you can select a row or any range in the spreadsheet using Apps Script. You do so with method setActiveRange(). An example follows below:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange('A1:D4');
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
Notice The method getDataRange() selects range "in which data is present.", so it won't select blank cells or the whole document. For such task, you need method getRange(row, column, numRows, numColumns) combined with getMaxRows() and getMaxColumns().
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
sheet.setActiveRange(range);
var selection = sheet.getSelection();
// Current cell: A1
var currentCell = selection.getCurrentCell();
// Active Range: A1:D4
var activeRange = selection.getActiveRange();
To do that you show in the image i use this:
function myFunction2() {
SpreadsheetApp.getActive().getRange( 2 + ':' + 2 ).activate();
};
and if you want to make it variable so, add a variable like n:
function myFunction2() {
var n = 2
SpreadsheetApp.getActive().getRange( n + ':' + n ).activate();
};
the way to manage range, rows and columns, are in the other answer.