How to Programatically Access Google Spreadsheet File Name - google-apps-script

Question is simple. How to get Name (File name in Google Drive) of a spreadsheet using spreadsheet identifier. If you want to know why I need it, you may please continue reading further.
I have created two type of spreadsheets to manage my financial books with my friends, family and business.
Type 1: A book that contains all transactions with a specific person and tells Amount owed or amount lended. I have about 15 of such sheets. Check this screenshot:
Type 2: A sheet named as investments. It fetches data from all other sheets using Sheet Identifier and displays total amount invested or borrowed. Check this screenshot:
In Type 2 sheet, I have programmed and made everything automated. You just need to paste Identifier of any new Spreadsheet in G Column and you're done. Only one thing is Killing me. I am unable to fetch name of the spreadsheet using Identifier. I fetched all records of the sheet using IMPORTRANGE Method, just the file name is not getting fetched. I tried plenty of functions.
Yellow Columns denotes Automatic Columns. I just want to make Column B automatic in case of Type 2 Spreadsheet. It should contain Spreadsheet File Name fetched through Sheet Identifier, same as I fetched all rows of multiple sheets & made calculations within Type 2 sheet. Thanks for considering to help me out.

If I understood your question correctly, you want the name of a Sheets file from the Sheets ID? How about:
var spreadsheetId = 'YOUR_SPREADSHEET_ID';
var sheetName = SpreadsheetApp.openById(spreadsheetId).getName();

Related

Google Apps Script - When new row containing data with specific keywords is added to Sheet 1, move that to a new row in a new corresponding sheet

I am using Google Sheets to evaluate data from a Zoho Form. I would like to sort the raw data from spreadsheet 1 ('Raw Data') into 3 new spreadsheets depending on 2 specific keywords found in 2 different columns in 'Raw Data'.
For context - the keywords for the first column (column 'G') are:
Ordered
Stock
Preordered
the keywords for the second column (column 'F') are:
B2B
B2C
There are a total of 6 possibilites that need to be accounted for: ordered & B2B, ordered & B2C, stock & B2B, stock & B2C, preordered & B2B, preordered & B2C.
If for instance a form is submitted for 'Ordered' and 'B2B', then a specific succession of rows must be transferred to the corresponding 'Ordered' spreadsheet because of Zoho Forms' nature of staggering the submitted information horizontally in the 'Raw Data' sheet. These rows will change depending on which one of the 6 combinations is chosen in the Form.
The 3 new spreadsheets will be named according to the keywords found in column 'G'.
So far I have been working with a Query in the new sheets to pull the needed information from the 'Raw Data' spreadsheet, however this hasn't proven to work as intended as Zoho Forms creates a new row rather than filling an existing one, causing the query to skip the newly created row and therefore not pulling any data.
Here is the Query, maybe it'll help identify what I am looking for:
=IF('Raw Data'!G6="Ordered", IF('Raw Data'!F6="B2B",QUERY('Raw Data'!A6:DN6,"SELECT A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T ORDER BY C desc LIMIT 1"), QUERY('Raw Data'!A6:DN6,"SELECT A,B,C,D,E,F,G,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK,AL,AM,AN,AO,AP,AQ,AR,AS,AT,AU ORDER BY C desc LIMIT 1")))
I have found someone who posted a Script similar to what I am looking for, however it is missing that conditional aspect to it. Here is the script, maybe it can serve as a good starting point. (all credit for this code goes to Mike Steelson):
function moveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var rng = ss.getSheetByName('Sheet1').getRange('A2:C2')
ss.getSheetByName('Sheet2').getRange(ss.getSheetByName('Sheet2').getLastRow()+1,1,1,3).setValues(rng.getValues())
rng.clearContent()
}
I am quite new to building these sort of things so please forgive me if the Query and my overall explanation isn't the best, if you have any questions I'll try my best to elaborate!
Cheers,
Rupert.

Copying and pasting data in Google Sheets to another column

I have a dashboard that is reporting live data based on a growing customer database, and I am currently tracking the daily process by copying and pasting the data in the column into the next on a daily basis.
I'd like to create a script that automatically copies the values from an array (B:B) for example, into another sheet OR into the next available column to the right.
You may refer to the image below for further clarity.
Sample
I saw may threads on this but they were all about copying into a different row, as opposed to a different column.
Many thanks in advance!
The getLastRow() method is used to return the position of the last row that has content.
The getLastColumn() method is used to return the position of the last column that has content.
Therefore, in order to be able to copy the data into the next available column, I suggest you try this:
function copyColumn() {
var ss = SpreadsheetApp.openById("ID_OF_THE_SPREADSHEET");
var sheetFrom = ss.getSheetByName("SHEET_FROM");
var sheetTo = ss.getSheetByName("SHEET_TO");
var values = sheetFrom.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS, 3, sheetFrom.getLastRow(), 1).getValues();
sheetTo.getRange(NO_OF_THE_ROW_WHERE_DATA_STARTS,sheetTo.getLastColumn()+1,values.length,1).setValues(values);
}
The above script localizes all the data that needs to be copied by using the getRange() method and is copied by using getValues().
NO_OF_THE_ROW_WHERE_DATA_STARTS is the value representing the number of the row where your data starts (in your case the row of No of LP - Secured);
3 is the value representing the C column (where the data you want to be copied is located);
sheetFrom.getLastRow() is the value representing the end of the data you want to be copied;
1 is the value representing the number of columns that need to be copied.
Afterwards, the getRange() method is used again in order to be able to identify where the data needs to be pasted and setValues() in order to actually paste it.
Note: The above script works for different spreadsheets and/or different sheets OR for sheets in the same spreadsheet. If you want to use it for the latter case, you just have to put the name of your sheet instead of SHEET_FROM and SHEET_TO.
Moreover, I suggest you check the following links since they might be of help:
Sheet Class Apps Script - getRange();
Range Class Apps Script - getValues();
Range Class Apps Script - setValues();
Sheet Class Apps Script - getLastColumn();
Sheet Class Apps Script - getLastRow();

Get the previous date from employee record with reference for the current date using 2 sheets [Google Sheets]

I have an excel file and need to update them to Google sheets. The problem is I cannot convert the functions in my excel to Google sheets.
Can anyone help me get the correct function for previous date from employee record with reference for the current date using 2 sheets [Google Sheets]
Excel function:
=AGGREGATE(14,6,Sheet2!K:K/(Sheet2!K:K<X2)/(Sheet2!A:A=A2),1)
I have tried QUERY function but I haven't found any examples online that could get the correct result.
From the 1st sheet 'Main Data to LOAD' I need to adjust column 'G' and 'I' it to get result.
Sheet 2 Employee record history used for checking the correct date for the first sheet.
Here is a mock sheet so anyone can try: (Kindly make a copy first for testing, I have an extra copy but just to be safe. Thanks)
https://docs.google.com/spreadsheets/d/16pZgf7igkt27Ep8a94-5t1M8KEW5wxP5uzSdHCNx_k4/edit?usp=sharing
Let me know if more information is needed.
Kind regards,

How to create a script to merge several sheets in another one?

Sory for my English, this is not my strength!
I am working for a NGO that charges training sessions.
We have a google sheet that compiles datas about participants at each training days one by one in different spreadsheets.
So my yearly document is composed of something like 30 spreadsheets.
I would like to create one more spreadsheet that would merge all the data from other tabs in order to :
- be able to see quickly who has not payed yet his bill
- calculate the total amount made
I know I can do it by using the Filter function but it is really time-consuming for so many spreadsheets and if I add a training session I would have to change the formula. Moreover, I will be forced to do it each year.
So I would like to create a button to import all the data in a new spreadsheet.
You will find an example of the sheet with only 2 spreadsheets here :
https://docs.google.com/spreadsheets/d/1-RxzUGJFXnU3_mJ3Qj0MCTpwPTlnmgIT439AchyrRrE/edit?usp=sharing
I hope you would be able to help me!!
Thanks and happy new year :D
Merging Sheets in another sheet in same spreadsheet.
You will need the id for spreadsheet named 'CopiDdeFCCE' the other parameter will default to 'CopieDdeFCCE'.
function mergeSpreadsheet(ssid,shname){
var shname=shname || 'CopieDdeFCCE';
var ss=SpreadsheetApp.openById(ssid);//id of spreadsheet named 'CopieDdeFCCE' in your case
var sh=ss.getSheetByName(shname);//sheetname of sheet where other sheets are merged into again in your case its named 'CopieDdeFCCE'
if(!sht){ss.insertSheet(shname);}
var allshts=ss.getSheets();
for(var i=0;i<allshts.length;i++){
if(allshts[i].getName()!=shname){//do this for all sheets except shname
var shi=allshts[i];
var rg=shi.getDataRange();
var vA=rg.getValues();
shi.getRange(sh.getLastRow() + 1, 1,shi.getLastRow(),shi.getLastColumn()).setValues(vA);
}
}
}
Just to be clear.
What Google calls the spreadsheet is the file that contains all of the tabs (i.e. sheets).
In your first comment you said the following: I would like to merge all the 28 sheets of a file called "CopieDdeFCCE" to a new sheet of the same file.
But now in your question you say: I would like to create a button to import all the data in a new spreadsheet.
You stated in your last comment that your command looks like this: mergeSpreadsheet("1KC6kHcgtLZ93S4-r4wOwHFOG6Rq3mesGRKv26Ttnm9E","CopieDdeFFCE")
But clearly that's not the id of the example and the example does not have a sheet with that name.
So my question is: What exactly do you want?
Very sory I did not receive any notification of your reply because it was an edit and when I came to see if you wrote something I looked at the bottom of the site. :/
First of all, happy new year and thank you again for your help!!
Here are the precisions you asked :
1° Indeed, I would like to merge all the 28 sheets of a file called "CopieDdeFCCE" to a new sheet of the same file. To be more accurate, I give you the link of the exact location of the document with some examples of sheets: link.
2° Secondly, I will create a button to update all the data easily. But I think I know how to do that, so forget that question for now ;)
3° In the document I put enclosed, do we agree that the "id" is : 1KC6kHcgtLZ93S4-r4wOwHFOG6Rq3mesGRKv26Ttnm9E ??
Thanks again for all your time!

Assign different user for editing rows based on criteria in Google spreadsheet

I am very new to this forum as well as to the google spreadsheet, while using this spreadsheet and sharing this spreadsheet with different collaborators, i need the following help as:
Data Available:
In one sheet the name of the collaborators are given with there e-mail IDs, against the dealers code, e.g. In Col A: Dealership Code, Col B: Name of the Collaborator, Col C: e-mail ID of the collaborator
In another sheet2 the database of dealers are given, like Col A:Dealership Code, Col B: Name of the collaborator, Col C: Status (a variable, which is to be edited by the Collaborator).
Required:
What is required in the Sheet2, only the collaborator against the dealership code can edit the status in Column C (after sharing of the spreadsheet), other rows of the spreadsheet remains protected, and the collaborator may not be able to change the status of other collaborator.
I'll give the same answer as in the Docs forum : you cannot protect a single row or even a single sheet in a spreadsheet. I suppose you'll have to re-think your approach differently to get a working solution with the existing tools.
Why not, for example, have a non-shared spreadsheet for every collaborator that synchronizes some data with a common spreadsheet ? You would have the same functionality, wouldn't you ?