Appscript to get hyperlink to another google sheet - google-apps-script

I am looking for a code that will help me to find the hyperlink of another google sheet on my google drive, everything i have read explains how to get the link for the active sheet..
any ideas?
Thanks a lot

You need to use DriveApp with functions getFilesByName or getFilesByType. Then provide file id to SpreadsheetApp.openById.

This look will give you the hyperlink to every sheet in a spreadsheet
SpreadsheetApp.openById(file.getId()).getSheets().forEach(s =>
tree.txt.push([ `=HYPERLINK("https://docs.google.com/spreadsheets/d/${file.getId()}/edit#gid=${s.getSheetId()}","${s.getName()}")`]));
It generates a column. It's just a fragment of a piece of code

I have something a bit more simple to apply. The code below goes into the Drive folder and list down every file (doc/sheets/pdf etc) and its hyperlink.
All you will need to change is your tab name to store the folder details and your Gdrive folder ID.
If you want to extract folders within a folder in the future you can switch getFiles() to getFolders().
function folderitems(){
const ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 1'); //Edit tab name to list the folder details
const parentFolder=DriveApp.getFolderById('INPUT FOLDER ID HERE'); //Input Gdrive ID here
const contents=parentFolder.getFiles();
ss.appendRow(['name','link']);
while (contents.hasNext()){
var file = contents.next();
var name=file.getName();
var link=file.getUrl();
ss.appendRow([name,link]);
}
}
Outcome should look something like this.

To retrieve the URL of one spreadsheet by script
function getFileUrl() {
var nameOfFile = 'your file name'
var files = DriveApp.getFilesByName(nameOfFile);
while (files.hasNext()) {
var file = files.next();
console.log (file.getUrl())
}
}

Related

Update Google Doc Files With Data Pulled From a SpreadSheet Using Doc File Name

I need to assign a membership number to each request I receive from aspiring members of my association.
Aspiring members fill a Google Docs file. When their requests are validated, those files need to be updated with the assigned membership number.
Inside the spreadsheet I have two columns. One contains the membership number and the other contains the ID of the request.
Spreadsheet Screenshot
The doc files are named with the ID of the request as well. The membership numbers should be added in place of the placeholder {membershipnumber} found in the doc files.
Doc File Screenshot
Is anyone able to help?
Thanks a lot for your precious help!
I really have no idea how to accomplish this.
I believe your goal is as follows.
You have Google Document files in the specific folder and have a Google Spreadsheet including the values for updating the Google Document files.
You want to replace the value of {membershipnumber} with the values of column "A" by searching the filename using the value of column "C".
In this case, how about the following sample script?
Sample script:
In this sample, please copy and paste the following script to the script editor of Google Spreadsheet and set the variables. And, save the script.
function myFunction() {
const folderId = "###"; // Please set your folder ID of the folder including the Google Document files.
const sheetName = "Sheet1"; // Please set your sheet name.
const folder = DriveApp.getFolderById(folderId);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
const obj = sheet.getRange("A2:C" + sheet.getLastRow()).getDisplayValues().reduce((o, [a, , c]) => (o[c] = a.trim(), o), {});
const docs = folder.getFilesByType(MimeType.GOOGLE_DOCS);
while (docs.hasNext()) {
const file = docs.next();
const filename = file.getName().trim();
if (obj[filename]) {
const doc = DocumentApp.openById(file.getId());
doc.getBody().replaceText("{membershipnumber}", obj[filename]);
}
}
}
When this script is run, the Google Document files are updated using the values of column "A" by searching the Document files with the filename retrieved from column "C".
References:
reduce()
getFilesByType(mimeType)
replaceText(searchPattern, replacement)

Change PDF file name with names from list with Google Scripts

I have a list of new filenames that like to be applied to a list of files in google drive.
I have the FileID and corresponding new filenames in a google sheet (column1 with FilesIDs, column2 with the new filenames)
The solutions i found all had to do with renaming from Old-name to New-name.
I'm looking for a method to rename google drive files using the FileID
What would the script look like? Many thanks in advance!
function changenames() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getActiveSheet();
const dt=sh.getRange(2,1,sh.getLastRow()-1,2).getValues();
dt.forEach(function(r){DriveApp.getFileById(r[0]).setName(r[1])});
}

Move folder matching a specific name into another folder?

Could someone provide me with a simple example how to use App Script to move a folder matching a specific name (not ID) into another folder inside Google Drive?
I have searched the existing threads but couldn't find anything specific to matching a specific folder name.
I believe your goal as follows.
You want to move a folder to other folder in your Google Drive using Google Apps Script.
For this, how about this answer? In your case, when the parent folder of the folder is moved, all files and sub folders in the folder can be moved. So the script can be simpler than the script for copying files. This has already been mentioned by Cooper's comment.
The sample script is as follows. As the input values, the source folder name and destination folder ID are used.
Sample script:
In this sample script, Drive service is used.
function myFunction() {
const sourceFolderName = "###"; // Please set the source folder name.
const destinationFolderId = "###"; // Please set the destination folder ID.
const src = DriveApp.getFoldersByName(sourceFolderName);
if (src.hasNext()) {
const srcFol = src.next();
srcFol.getParents().next().removeFolder(srcFol);
DriveApp.getFolderById(destinationFolderId).addFolder(srcFol);
}
}
Note:
When Drive API is used, the script becomes as follows.
function myFunction() {
const sourceFolderName = "###"; // Please set the source folder name.
const destinationFolderId = "###"; // Please set the destination folder ID.
const src = DriveApp.getFoldersByName(sourceFolderName);
if (src.hasNext()) {
Drive.Files.patch({parents: [{id: destinationFolderId}]}, src.next().getId());
}
}
References:
removeFolder(child)
addFolder(child)

How to open a spreadsheet only by name of the corresponding file in Google Sheets?

I need a way to open a spreadsheet only by having the name of the corresponding file and the folder where the file is located. What I'm trying to do is creating a temporary copy of an active spreadsheet and reading information from the copy file rather than the active one. But unfortunately this copy file won't have a constant ID and URL as it's going to be erased every time after the script is done, and re-created with the next running of the script.
What I already tried is using the URL of the temporary file in order to open the spreadsheet itself, as you can see below.
But it's not useful for me, as I don't have a constant URL of this file (as it will be erased in the end of the script and have a new URL with the next script). But I have a constant name of the file, as well as this will be the only file in the respective folder.
What I also tried is looking for methods to open file only using its name (which is "temp_copy" in a 'temp_folder'). But I didn't find a working solution.
var report = SpreadsheetApp.getActiveSpreadsheet();
var temp_folder =
DriveApp.getFolderById("1EAsvVcFjIw5iGTeF_SxxQJLVlaLHl_pR");
DriveApp.getFileById(report.getId()).makeCopy("temp_copy", temp_folder);
var temp_copy = SpreadsheetApp.openByUrl(' ');
var hiddenSheet = temp_copy.getSheetByName('DATE');
var lastRow = hiddenSheet.getSheetValues(1,1,1,1);
var sheet = temp_copy.getSheetByName('forCSV');
var lastColumn = sheet.getLastColumn();
var activeRange = sheet.getRange(1,2,lastRow,lastColumn);
What I'm expecting as a result is to make the link between the 3rd line of the code (when making a copy of the basic file) and 5th line of the code onwards (when referencing specific range from the temporary file) only by using name of the file or respective folder. In theory it seems so easy, I should be missing something somewhere...
Many thanks in advance!
You can use DriveApp to get a FileIterator which contains the Sheet.
var file, files = DriveApp.getFilesByName("temp_copy");
if (files.hasNext ()){
file = files.next(); // Get first result for name
} else {
return "";
}
var sheet = SpreadsheetApp.openById(file.getId())

Get Current Google Script Name to a Variable

Does anyone know a way to get the name of the current google script file that's bound to the sheet you are using and write it to a variable? I want to put it into a sheets cell to keep track of changes I make.
Cheers
var id = ScriptApp.getScriptId();
var name = DriveApp.getFileById(id).getName();
Logger.log(name);
For standalone Google Scripts (not bound to any Google Sheet or Docs), you can use the DriveApp service to get the file name from Drive.
Right-click the file in Google Drive and copy the Share Link. Then get the ID from the shared link and use the DriveApp service to retrieve the name from the file ID.
function getName() {
var fileId = "123";
var file = DriveApp.getFileById(fileId);
return file.getName();
}