I've been reading this without making any headway:
https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app
Is there a way to programmatically get the Spreadsheet ID and Sheet ID of a particular Google SpreadSheet's sheet without manually typing or copying the URL?
1.) If the script is bound to the spreadsheet then you can just use getID and getSheetID as mentioned by TheMaster in the comments. See codes below:
function getIDs() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var spreadSheetID = ss.getId();
var sheetID = sheet.getSheetId();
console.log(spreadSheetID);
console.log(sheetID);
}
Result:
Take note that the first sheet always has an ID of 0 (gid=0) as the default value.
2.) Otherwise if what you are trying to do is get the Spreadsheet ID and Sheet ID from different spreadsheets it is not possible without copying the URL since there is no way it can identify which spreadsheet to refer to.
3.) Suggestion
What you can do if you need to get IDs of different files is to move them to a folder in your drive, then you can use the code below:
function listFilesInFolder(folderName) {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1,1,sheet.getLastRow(),sheet.getLastColumn()).clearContent();
sheet.appendRow(["Name", "Spreadsheet ID", "Sheet ID"]);
//change the folder ID below to reflect your folder's ID (look in the URL when you're in your folder)
var folder = DriveApp.getFolderById("HIS_SHOULD_BE_YOUR_FOLDER_ID");
var contents = folder.getFiles();
var cnt = 0;
var file;
while (contents.hasNext()) {
var file = contents.next();
cnt++;
data = [
file.getName(),
file.getId(),
];
var spreadsheetSheets = SpreadsheetApp.openById(data[1]).getSheetByName("Sheet2").getSheetId();
data.push(spreadsheetSheets);
sheet.appendRow(data);
};
};
Folder:
Result:
Note: In my example code I've made use of Sheet2 to see that it is working as expected. Please set accordingly to the name of the sheet which you are trying to get the ID.
Let me know if this works for you or if you have other questions.
References:
Are Google Spreadsheets' first sheets always given an ID of 0?
Getting all files ID in drive folder
Related
I am looking for an app script that lets me save a sheet as PDF from my Google Workbook to a specific folder in Google Drive based on a condition.
In my Google Drive I have a Google Sheet Workbook file and an employee folder.
The employee folder Id is 1Jy2Yr7u0qrgy90Gvtb6p8V27baS7T_eP
My google workbook has a sheet called 'Sheet2' whose Id is 1DmDBmEijUkolVp7aVgGNK4cTRu5y4uLdzbCrDYO5XRA.
This sheet has a cell C3 that has an employee name. The value in this cell needs to be checked, i.e. if the employee name matches then the script should convert the sheet to a PDF format and place the PDF in employee folder.
I am not a programmer or a coder but doing my best to get this working. I went through many scripts that I found here in stackoverflow and in Youtube and came up with this. I am not sure if it is correct. Need some expert help. Hoping someone can help.
function checkSheet() {
var sheetName = "1DmDBmEijUkolVp7aVgGNK4cTRu5y4uLdzbCrDYO5XRA";
var folderID = "1Jy2Yr7u0qrgy90Gvtb6p8V27baS7T_eP";
var sourceSpreadsheet = SpreadsheetApp.getActive();
var sourceSheet = sourceSpreadsheet.getSheetByName(sheetName);
var folder = DriveApp.getFolderById(folderID);
var checkForNews = spreadsheetApp.getActiveSpreadsheet().getSheetByName("Tracking").getRange("c6").getValue();
if (checkForNews='Yes') {
var destSpreadsheet = SpreadsheetApp.open(DriveApp.getFileById(sourceSpreadsheet.getId()).makeCopy("tmp_convert_to_pdf", folder));
var theBlob = destSpreadsheet.getBlob().getAs('application/pdf').setName(pdfName);
var newFile = folder.createFile(theBlob);
}
}
Here are the resource that I found,
Using Google Apps Script to save a single sheet from a spreadsheet as pdf in a specific folder
How to check for cell value?
How can I modify this to save a spreadsheet into a new folder
As mentioned above I came up with that script which I think is not right. Please help.
Try it this way:
function checkSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Tracking");
var fldr = DriveApp.getFolderById("folder id");
if(sh.getRange("C6").getValue() == "Yes") {
var theBlob = ss.getBlob().getAs('application/pdf').setName("mypdf");
fldr.createFile(theBlob);
}
}
I am creating a copy of my google spreadsheet, and now want to close the current spreadsheet and open the one i created a copy. I know i can open it using the sheet id, but i dont know the sheet id yet because I want to achieve this using one macro!
current code -
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
//want to open this copied file
}
By modified your code a little bit, then you can obtain the copy sheet Id with newsheet.getId() function:
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxV6N-A4Z77f9OijHtAe2VhAU");
var newsheet = DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
Logger.log(newsheet.getId())
}
Say, I have 100 google spreadsheets file and I need to merge into one file with 100 tabs(sheets). Is it possible with app script. Currently I have a code below,
function popForm() {
var ss = SpreadsheetApp.getActive();
for(var b = 0; b<100; b++){
var sheet = ss.getSheetByName('Sheet '+b);
var numberRows = sheet.getDataRange().getNumRows();
....
....
The above code will work with spreadsheets with 100 tabs(Sheets). But I have 100 sreadsheets with one tab(Sheet).
Explanation:
You have a folder with FolderId that contains all the spreadsheet
files. You find this folder using the getFolderById() method.
You get the files of this folder using the getFiles() method and
you use a while loop to iterate over these files.
For every file, you get the first sheet and you copy it using the
copyTo() method to the source (active) spreadsheet file.
The newly create sheet will be named after the name of the spreadsheet file that it came from. Feel free to change this part or let me know so I can modify it.
Solution:
You must be looking for this:
function myFunction() {
const source_sh = SpreadsheetApp.getActive();
const folder = DriveApp.getFolderById('FolderId');
const files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var target_ss = SpreadsheetApp.openById(file.getId());
var target_sh = target_ss.getSheets()[0];
target_sh.copyTo(source_sh).setName(file.getName());
}
}
I am trying to create a Google Sheet template where a user can enter a value (Project URN) in a cell and run a script to save the template as a sheet, renamed with the Project URN.
I have created a test Google Sheet here https://docs.google.com/spreadsheets/d/1IPpQyYHl_TtNa1lGpmu4dlzwTFjOotV2OcnxFT84iUk/edit?usp=sharing
This sheet has the following script, but I cant get the renaming function to correctly work.
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B7RrbZuJGLlha3NvM3ZqcTY3MDA");
DriveApp.getFileById(sheet.getId())
.(makeCopy(values = SpreadsheetApp.getActiveSheet()
.getRange(2, 2).getValues()), destFolder);
} //END function saveAsSpreadsheet
I did also find the following article but wasn't able to turn it to my use. However, the secret must be here somewhere! Rename worksheet to cell value keeping format
Kitten
You almost have it. Please try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
var id = ss.getId(); // Get spreadsheet ID
var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet to be created
var sheet_name = sheet.getRange("A1").getValue(); // Get the value, in this case: Project Urn
var folder = DriveApp.getFolderById("XXXX"); // Get the folder where the sheet needs to be placed.
sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.
}
Let me know if you have any questions.
I have a google spreadsheet that is keeping track of the files and their names that are inside the folder where the spreadsheet is.
I have looked and can not find how to get the location or ID of that sheet
In MS CMD script it would be "%~dp0".
How to do this in google apps spreadsheet script.
function FnctnMenuUpdateGetLocation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSID=ss.get.getId();
var file = DocsList.getFileById(SSID);
Browser.msgBox(SSID);
Browser.msgBox(add);
}
https://docs.google.com/spreadsheet/ccc?key=0AmsQN3N9km70dGwtLTJXVVBzbmJhdmE5OFpkbTVxelE&usp=sharing
ya, I am working in it
this is a live sheet
From what I have how do you get the address of the spreadsheet in google drive
from the spread sheet I will be having it add folder/dir and file that I need
since google docs is not google drive how is the interface from Spreadsheet to the google drive
This works as of June 2018
This answer also assumes that you are only interested in the first folder in the list of parents.
var spreadsheetId = SpreadsheetApp.getActiveSpreadsheet().getId();
var spreadsheetFile = DriveApp.getFileById(spreadsheetId);
var folderId = spreadsheetFile.getParents().next().getId();
Both the Document Service and Drive Service can report which folder(s) a spreadsheet is in.
Here's your function, updated to show the name of a folder that contains the spreadsheet, using both services.
function FnctnMenuUpdateGetLocation() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var SSID=ss.getId();
var fileInDocs = DocsList.getFileById(SSID);
var folderInDocs = fileInDocs.getParents()[0].getName();
var fileInDrive = DriveApp.getFolderById(SSID);
var folderinDrive = fileInDrive.getParents().next().getName();
Browser.msgBox("Docs says: " + folderInDocs +
", Drive says: " + folderinDrive);
}
Document Service's .getParents() method returns an array of folder objects, while Drive's returns an iterator, so the details of how to retrieve a specific parent folder(s) once you've got all of them differs.
Note that a spreadsheet may be "contained" in multiple folders... in this case, we've assumed that the first folder in the list is the only one - that assumption may be invalid.