I have a spreadsheet with a lot of data. I need to retrieve this data column-wise (or row-wise if column-wise is too complicated). However I can't figure out a way to do this. I have the following working function which returns the data contained in a specific rectangular grid in the spreadsheet specified by row number, column number, number of rows, number of columns.
However, what I want to do, is simply mention the column number and get all the data in that column.
function getSpreadSheetData(){
var id= SpreadsheetApp.getActiveSpreadsheet().getId();
var ss = SpreadsheetApp.openById(id);
var sheets = ss.getSheets();
// the variable sheets is an array of Sheet objects
var sheetData = sheets[0].getRange(row, column, numRows, numColumns);
//consider row,column,numRows,numColumns = 2,1,2,2
return sheetData;
}
How should I modify this code?
How do you want to return it and to what?
it seems what you are trying to achieve can easily be done with:
function getSpreadSheetColumns(sheetName, c){
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
return ss.getRange(1, c, ss.getLastRow()).getValues();
}
Possibly moving the sheet name inside the function if it will always be the same.
Related
I have a large google sheet with 30275 rows and 133 columns in a google sheet. I want to filter the data and copy column AZ to another spreadsheet.
Link to spreadsheet: https://docs.google.com/spreadsheets/d/1aiuHIYzlCM7zO_5oZ0aOCKDwPo06syXhWvhQMKgJE2I/edit?usp=sharing
I have been trying to follow this link
I am not that familiar with javascript and the code is designed to exclude items from filter rather than including items on filter. I have 500+ items to exclude so need to work out something that will be efficient in filtering large dataset in short time before execution limit is reached.
Here is my code so far. Any help to get this working would be appreciated.
NOTE: Filter/ Query with importrange formulas dont work due to the large volume of data. So I need an efficient script to filter large dataset and move them to another sheet before execution time limit.
function filtered() {
var ss = SpreadsheetApp.openById('1u9z_8J-tvTZaW4adO6kCk7bkWeB0pwPcZQdjBazpExI');
var sheet = ss.getSheetByName('Sheet1');
var destsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('JockeyList');
var demosheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Betting data - Demo');
var jockey = demosheet.getRange('L14').getValues();
// Get full (non-filtered) data
var values = sheet.getRange('A:EC').getValues();
// Apply filter criteria here
//Logger.log(jockey);
var hiddenValues = jockey;
values = values.filter(function(v) {
return hiddenValues.indexOf(v[51]) == 1;
});
Logger.log(values.length);
// Set filtered data on the target sheet
destsheet.getRange(10, 5, values.length, 2).setValues(values);
}
Ok so it seems like you want to copy only the values from AZ in 'Sheet1' that are equal to whatever string value is contained in cell L14 of sheet 'Betting data - Demo.' If that is the case, then here is a change to your original code that will accomplish that:
function filtered() {
var ss = SpreadsheetApp.openById('1u9z_8J-tvTZaW4adO6kCk7bkWeB0pwPcZQdjBazpExI');
var sheet = ss.getSheetByName('Sheet1');
// this assumes that your destsheet and demosheet are in the same spreadsheet. Keep in mind that opening spreadsheets with SpreadsheetApp is costly so you want to minimize your calls to new sheets.
var destsheet = ss.getSheetByName('JockeyList');
var demosheet = ss.getSheetByName('Betting data - Demo');
var jockey = demosheet.getRange('L14').getValue();
var searchTerm = new RegExp(jockey);
// Get full (non-filtered) data
var values = sheet.getRange('A:EC').getValues();
// Apply filter criteria here and return ONLY THE VALUE IN COLUMN AZ
var filteredValues = values.reduce(function(resultArray, row) {
if (searchTerm.test(row[51])) resultArray.push([row[51]]);
return resultArray;
}, [])
// Set filtered data on the target sheet
// Note* not clear why you are starting at row 10, but as is this will overwrite all of the data in column 5 of destsheet starting from row 10 every time this function runs
destsheet.getRange(10, 5, filteredValues.length, 1).setValues(filteredValues);
}
As it says in the code sample, this will only copy and paste the value in column AZ of 'sheet1'. It does not alter 'sheet1' in any way. If that is really all you want to do, then this function works, but it's overkill. Since you are just filtering values in AZ against a single string value, it would be more efficient to simply count the number of times that string occurs in column AZ and then add the string that number of times to your destination sheet.
Also note that your original function is pasting values into destsheet into a constant row (row 10). That means that every time your function runs, the data from row 10 on will be overwritten by whatever data you are copying from 'sheet1'
Hi I have a working program to pull data from one sheet to another sheet and place in the relevant cells, however with one column I want to change the results which reads a "K" from the source sheet and write an "A" to the target sheet using a if function but I can only seem to do one cell and not the full column. How can I do this for multiple cells? If I try.getRange("F18:41") it doesn't seem to work.
Please see the code below:
var sss = SpreadsheetApp.openById('....'); // sss = source spreadsheet
var ss = sss.getSheetByName('.....); // ss = source sheet
//Get full range of data sheet 1
var TypeWire = ss.getRange("F18");
//get A1 notation identifying the range
var A16Range = TypeWire.getA1Notation();
//get the data values in range
var SDataSixteen = TypeWire.getValues();
var tss = SpreadsheetApp.openById('.....'); // tss = target spreadsheet
var ts = tss.getSheetByName('....'); // ts = target sheet
//set the target range to the values of the source data
ts.getRange("C40:C61").setValues(SDataSeven);
if (SDataSixteen == "K")
{
ts.getRange("L16").setValue("A");
}
}
The getValues() method of Class Range returns a multidimentional (2D) array. The elements of the "outer" array are arrays that represent rows. The elements of the inner arrays are objects that represent the cell values for the corresponding row.
There are several ways to do what you are looking for. Perhaps the easier to understand is the technique shown in the Cooper's answer: Use nested for statements.
Use one for statement to iterate over the rows, then another for loop to iterate over the row cells.
IMPORTANT NOTE:
Using loops to write single cells values is very slow. For recommendations please read https://developers.google.com/apps-script/guides/support/best-practices
Related
Hide Row in Google Sheets if Cell Contains "no" - Multiple Sheets
How to compare values of cells in if condition?
I have a problem where I need to transfer the cell data from one sheet to another. What I'm trying to achieve in the end is that if I click a button, it will import my data from the sheet one sheet to the sheet I'm currently using.
My original code looked like this:
function C2() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C2");
var destSheet = SpreadsheetApp.getActiveSpreadsheet()
sourceSheet.getRange("A1:I43").copyTo(destSheet.getRange("A1:I43"), {contentsOnly:true});
}
But that won't work for the new modifications I've made. It's erasing the formulas in the cells on the next sheet and replacing them with the values of the previous sheet that the formulas produced.
I've tried to use this formula, selecting all of the areas that I'd like to transfer, but it results in a "range not found" error message.
function C1() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C1");
var destSheet = SpreadsheetApp.getActiveSpreadsheet()
sourceSheet.getRange("A2,A4:B16,A21,A23,A25,A27,D3:D22,E4:E22,F3:H22,C24,C26:E31,F25,L1").copyTo(destSheet.getRange("A2,A4:B16,A21,A23,A25,A27,D3:D22,E4:E22,F3:H22,C24,C26:E31,F25,L1"), {contentsOnly:true});
}
If it's possible to cherry pick the cells I need and transfer them to my desired sheet instead of only using a broad range (ex. "A1:I43"), That would be ideal. Any and all help is very much appreciated.
You need to define a RangeList and iterate through all the contained ranges.
If you want to copy values only, getValues() and setValues() will be useful methods for you:
function myFunction() {
var spreadsheet=SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("C2");
var destSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var rangeList=sourceSheet.getRangeList(["A2","A4:B16","A21","A23","A25","A27","D3:D22","E4:E22","F3:H22","C24","C26:E31","F25","L1"]);
var rangeList2=destSheet.getRangeList(["A2","A4:B16","A21","A23","A25","A27","D3:D22","E4:E22","F3:H22","C24","C26:E31","F25","L1"]);
for (var i=0;i<rangeList.getRanges().length;i++){
rangeList2.getRanges()[i].setValues(rangeList.getRanges()[i].getValues());
}
}
I`m trying to replace old values with new values using setValues in Google sheet script.
The data is in the below link...
https://docs.google.com/spreadsheets/d/1pSUVkxM9FhSNgizedHbY2MnYGTnC2iiYLfrWsoPmDks/edit?usp=sharing
I`m basically trying to remove first 14 characters and the last 12 characters under "Tracker" column
Below is the code I tried..
function URLReplacement() {
var ss = SpreadsheetApp.getActive().getSheetByName("transformer");
var rng = ss.getRange("G:G");
var data = rng.getValues();
for (var items in data)
{
var newer = data[items][0].substring(14)
// Turn these strings into an array
var newerr = newer.split(" ")
// Turn this into 2 dimensional array to use setValues
ss.getRange("G:G").setValues([newerr])
Logger.log([newer]);
}
}
But now, I get errors with the setValues statement
Saying the range I set there do not match the data
What am I doing wrong here..?
Can anyone please provide me with suggestions / advice?
You want to convert from IMAGE_SUFFIX_"http://google.com"<xxxnouse>" to http://google.com at the column "G".
The format of IMAGE_SUFFIX_"http://google.com"<xxxnouse>" is constant.
If my understanding is correct, how about this modification? The reason of your error is that [newer] is not 2 dimensional array for using setValues(). If this error was removed, the header is removed by overwriting the empty value. So I would like to modify as follows.
Modification points:
When getLastRow() is used, the data size retrieved by it can be reduced from that retrieved by "G:G". By this, the process cost can be reduced.
Header is not retrieved by getRange(2, 7, ss.getLastRow(), 1).
From the format of IMAGE_SUFFIX_"http://google.com"<xxxnouse>", split() was used for parsing this value.
The converted data was put by setValues(). By this, the process cost can be also reduced.
Modified script:
function URLReplacement() {
var ss = SpreadsheetApp.getActive().getSheetByName("transformer");
var rng = ss.getRange(2, 7, ss.getLastRow(), 1); // Modified
var data = rng.getValues();
var convertedData = data.map(function(e) {return e[0] ? [e[0].split('"')[1]] : e}); // Added
rng.setValues(convertedData); // Added
}
Note:
In your shared sample Spreadsheet, the sheet name is "Sheet1". But your script uses "transformer" as the sheet name. Please be careful this.
If the format of actual values in your Spreadsheet is different from your shared Spreadsheet, this might not be able to be used.
References:
split()
setValues()
If this was not the result you want, I apologize.
I'm new to Google Apps Script and I'm trying to make a script where I'll take a single string value and copy to multiple rows in a google sheet. I've taken an array to save the single value multiple times. But still I can't get it done. Every time I run the script, I get this error,
Cannot convert Array to Object[][]
Here are my codes,
function myFun() {
var ss = SpreadsheetApp.openById(SHEET_ID);
var sheet = ss.getSheetByName("Form Responses");
var new_vals = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
var master_ss = SpreadsheetApp.getActiveSpreadsheet();
var master_sheet = master_ss.getSheetByName("Sheet1");
var lr = master_sheet.getLastRow()+1;
var ss_real_name = "District";
var ss_real_names = [];
for (var i=0; i<new_vals.length; i++)
{
ss_real_names.push(ss_real_name);
}
master_sheet.getRange(lr, 1, new_vals.length).setValues(ss_real_names);
}
Is there something wrong in my code? How can I save the single string value in multiple rows?
Google Apps script writes values as arrays of arrays with every array inside of the outer array being a row and the elements in the inner arrays going into the columns.
If you want to write the data as rows you need to create an array filled with one element arrays. Try ss_real_names.push([ss_real_name]);.
If you wanted to write them as a column vector you could just say setValues([ss_real_names]) instead.