I am trying to create a function that takes a spreadsheet and it contents and duplicate into a new one. This is my function:
function executeIt(){
var ss = SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1dH5Ehdn2sLd0gPLzdq77ntLUWYwbSwh1nxr1vD7FgQc/edit');
var dataRange = ss.getRange(1, 1, 3, 3);
var myData = dataRange.getValues();
var newSS = SpreadsheetApp.create('Myfile ');
newSS.getActiveSheet().getRange(1, 1, 3, 3);
Logger.log(newSS.getUrl());
}
But when i run it, it says an error code with the getRange();
Help please.
As you will see in the documentation, SpreadsheetApp.openByUrl(...) returns a Spreadsheet. Spreadsheet.getRange(a1Notation) takes a string representing the a1 notation of your range, not the start and end indices of your data.
Related
I'm trying to combine a new value to an existing value in Google Sheet. So when user input the codeInput then it will find the codeInput row and add formObject.things value that user just filled to the existing thingsList value (which the same row as codeInput). I tried this code but it return blank to the Google Sheet.
Your response would be very appreciated.
Here's my code:
function update1(formObject) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ws = ss.getSheetByName("Sheet1");
var data = ws.getRange(5, 1, ws.getLastRow()-1, 1).getValues();
var codeList = data.map(function(r){return r[0].toString(); });
var thingsList = data.map(function(r){return r[8]; });
var position = codeList.indexOf(formObject.codeInput);
var array = [thingsList[position], formObject.things]
if (position > -1) {
position += 5;
ws.getRange(position,9).setValue(array);
}
}
Regarding
var array = [thingsList[position], formObject.things]
and
ws.getRange(position,9).setValue(array);
The type of array is Array
setValue doesn't allow Array objects as argument. It accepts, string, number, Date and Boolean
Instead of setValue
you could pass a Google Sheets array (i.e. ={1,2}) by using setFormula
you could pass a bidimenstional Array to Google Sheets by using setValues
you could pass an array (unidimensional Array) by using appendRow
Regarding
var data = ws.getRange(5, 1, ws.getLastRow()-1, 1).getValues();
and
var thingsList = data.map(function(r){return r[8]; });
data is and Array possible having multiple rows but it has only one column, so, r[8] returns null which cause that thinkslist be an array of null values.
So far the question doesn't include enough details to know what could be the fix needed, but here are two possibilities among others :
var data = ws.getRange(5, 1, ws.getLastRow()-1, 9).getValues();
or
var data = ws.getRange(5, 1, ws.getLastRow()-1, ws.getLastColumn()).getValues();
I am struggling with the following task: I have an folder with 20 spreadsheets that have all a datatable with the same format (same columns). I want to loop through all of them, collect the data, combine it to one big array and display it on a spreadsheet.
However, I am struggling to combine the arrays. In the first step I load the column headers from the final sheet. Afterwards I loop through all files (I have a table with the sheets ID stored in the array aFiles) but I cannot combine the arrays. I tried it with aData.concat but it didn't do anything.
function getInformation(){
var ssZ = SpreadsheetApp.getActiveSpreadsheet();
var sZu = ssZ.getSheetByName("Meldeeinheiten");
var aFiles = sZu.getDataRange().getValues();
var sDa = ssZ.getSheetByName("Data_komplett")
var aData = sDa.getRange(1, 1, 1, 15).getValues()
for (var iFile = 1; iFile<aFiles.length; iFile ++){
var org = aFiles[iFile][0];
var name = aFiles[iFile][1];
var id= aFiles[iFile][2];
var ssI = SpreadsheetApp.openById(id);
var sData = ssI.getSheetByName("Data");
var lRow = sData.getLastRow();
if (lRow >= 2){
var aNew =[];
aNew = sData.getRange(2, 1, sData.getLastRow(), 15).getValues();
aData.concat(aData,aNew);
}
}
var lDRow = sDa.getLastRow();
sDa.getRange(2, 1, lDRow , 15).clear()
var rng = sDa.getRange(1, 1, aData.length, 15);
rng.setValues(aData);
Logger.log(aData.length)
}
The data in the tables is strucutred in the following way:
Spreadsheet A:
Org Name Hours Comment
A Joe 15 Weekend
A Pete 20 Sunday
A Maik 15 test
Spreadsheet B
Org Name Hours Comment
B Will 15 Monday
B Anna 18 holiday
B Dave 10 test
...
And so one.
Has anybody an idea how I can combine those data and create a "joint database"?
Issue:
aData.concat(aData,aNew);
Array.concat doesn't concat in-place. It returns the concatenated array.
Solution:
Use the returned array:
aData = aData.concat(aData,aNew);
Alternatively use flatMap,
const out = aFiles.flatMap(([org, name, id])=>
SpreadsheetApp.openById(id)
.getSheetByName('Data')
.getDataRange()//TODO Modify range according to last row
.getValues())
Firstly I am very inexperienced here so apologies if this is obvious.
I have an array of data that updates automatically and I wish to copy this data out so I have an archive of it.
I have managed to find the following script which works for what I want:
function saveInstaPostData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("xxx");
var data0 = sheet.getRange("xxx!A2:AQ21").getValue();
var data1 = sheet.getRange("xxx!B2").getValue();
var data2 = sheet.getRange("xxx!C2").getValue();
var data3 = sheet.getRange("xxx!D2").getValue();
sheet.appendRow([data1,data2,data3,...]);
}
However I have a range of 860 cells, so doing it one by one isn't too feasible.
Reading up on the appendRow method I realise that it (seems) to only be able to append one row at a time. I only have 20 rows so this should still be doable.
I then tried using
function saveInstaPostData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Insta faction post data");
var values = sheet.getSheetValues(2, 1, 1, 43);
sheet.appendRow([values]);
However this outputs the following in the first cell of the new row: [Ljava.lang.Object;#247f2c9a
This seems to be the array I'm trying to append (java: what is this: [Ljava.lang.Object;?) however I can't get it to work!
[I have also tried using
function saveInstaPostData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Insta faction post data");
var range = sheet.getRange(2,1,20,43);
var values = range.getValues();
sheet.appendRow([values]);
This outputs ' Range ' in the first cell of the new row.
]
Any pointers would be really appreciated,
Thanks,
On your example, you try to append a tri dimensionnal array:
var values = sheet.getSheetValues(2, 1, 1, 43);
or
var range = sheet.getRange(2,1,20,43);
var values = range.getValues();
Your "values" variable contains a bi dimensionnal array, that you've put in a mono dimensionnal array:
sheet.appendRow([values]); // [values] put bi dimensionnal array in a mono dimensionnal array
To append your data, you need to append each row of your array, one after a other like :
for(var i=0; i<values.length;i++){
sheet.appendRow(values[i]);
}
I'm working on a project that outputs a huge array, and I need to clean it up to send in an email. It seems the simplest approach is the split() function, but I can't seem to make it work.
function splitArray() {
var ss = SpreadsheetApp.getActiveSheet();
var row = ss.getLastRow();
var range = ss.getRange(row, 1, 1, ss.getMaxColumns());
var data = range.getValues();
data.split("\n");
Logger.log(data);}
You need to set the return value of data.split("\n"); to something.
You are splitting the value successfully but not storing that value anywhwere
Try:
data = data.split("\n");
or
Logger.log(data.split("\n"));
I am pretty new to this forum and also to the google scripts. I have never actually learned JavaScript or any other programming language, however I was forced to start using then since the time I chose the google apps as my main platform for my small business.
My problem is in google ImportRange function limitation to 50 on every spreadsheet. I created a model, where every customer has his own spreadsheet with his personal data, deadlines, etc. located in his own folder in google drive.
I also created a spreadsheet called "Organizer", Organizer has two functions -
1) create automatic correspondention using autoCrat script,
2) show deadlines for every costumer and sort them by priority.
So i need to be able to share / import / copy data from all customer spreadsheets to the "Organizer". Because there are 50+ costumer spreadsheets, I had to give up on ImportRange function and instead of that I am using a simple script to copy files from every spreadsheet directly:
function ImportDataRange() {
// rown number.1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Databaze");
var range = sheet.getRange(2, 1)
var id = range.getValue()
var ssraw = SpreadsheetApp.openById(id);
var sheetraw = ssraw.getSheetByName("Raw");
var range = sheetraw.getRange("A2:AB2");
var data = range.getValues();
sheet.getRange("B2:AC2").setValues(data)
// row number.2
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Databaze");
var range = sheet.getRange(3, 1)
var id = range.getValue()
var ssraw = SpreadsheetApp.openById(id);
var sheetraw = ssraw.getSheetByName("Raw");
var range = sheetraw.getRange("A2:AB2");
var data = range.getValues();
sheet.getRange("B3:AC3").setValues(data)
}
This script actually works well, but problem is, when I want to add new costumer spreadsheet to "Organizer" using this method, I have to manually add new copy of whole code for every new row and also change the output range of imported data and location of source file ID in "Organizer".
Does anybody know some kind of workaroud, which will help me to add new rows / costumer data easier / automatically ?
Thank you for your help!
You are going to want to use a loop of some sort. I haven't tested the code below, but based on your example, I think this should work for you.
function ImportOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Databaze");
// Get the first column
// lastRow() - 1 because we're starting at row 2
var sheetIds = sheet.getRange(2, 1, sheet.getLastRow() - 1);
// For each ID in the id column
for (var i = 0; i < sheetIds.length; i++) {
var id = sheetIds[i]; // Get the id from the Array
// Get the new sheet, range and values
var ssraw = SpreadsheetApp.openById(id);
var sheetraw = ssraw.getSheetByName("Raw");
var range = sheetraw.getRange("A2:AB2");
var data = range.getValues();
// Get the local range, and write the values
sheet.getRange(i + 1, 2, 1, data[0].length).setValues(data)
}
}
As you learn more about GAS and JS, take advantage of MDN and the GAS Documentation
I took the fooby´s code and after long trial-and-error procedure I came with code that is working for me, so if anyone is interested, here it is:
function ShowDataFromOtherSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Databaze");
var ids = sheet.getRange(2, 1, sheet.getLastRow() - 1).getValues();
for (var i = 0; i < ids.length; i++) {
var id = ids[i];
var ssraw = SpreadsheetApp.openById(id);
var sheetraw = ssraw.getSheetByName("Raw");
var range = sheetraw.getRange("A2:AB2");
var data = range.getValues();
sheet.getRange(i + 2, 2, 1, data[0].length).setValues(data)
}
}
And thank you fooby - i would not make it without your help!