Pages

luni, 28 iulie 2025

News : Google Apps Script - add link to google document.

Today, I tested how to add a link to google drive document.
I tried some issues with some source code and this issue not work !
Let's see some simple examples:
/**
 * Creates a new Google Docs document with a test link.
 * This script is intended to verify the functionality of setLinkUrl and document placement in a folder.
 */
function createDocumentWithTestLink() {
  const docTitle = "Document Test Link GAS";
  const doc = DocumentApp.create(docTitle); // Creates the initial document in My Drive
  const body = doc.getBody(); // Gets the body of the document to add content

  // Add a paragraph of text to the document body
  const paragraph = body.appendParagraph("This is a test link to Google.");

  // Define the test URL (ensure it's a complete URL with protocol, e.g., https://)
  const testUrl = "https://www.google.com";

  // Set the hyperlink for the entire paragraph
  try {
    paragraph.setLinkUrl(testUrl); // Applies the URL as a hyperlink to the paragraph
    Logger.log(`Link "${testUrl}" was successfully set on the paragraph.`);
  } catch (e) {
    Logger.log(`Error setting the link: ${e.message}`); // Logs any error during link setting
  }

  // --- NEW: Move the document to the same folder as the spreadsheet ---
  try {
    const spreadsheet = SpreadsheetApp.getActiveSpreadsheet(); // Gets the currently active spreadsheet
    const spreadsheetFile = DriveApp.getFileById(spreadsheet.getId()); // Gets the Drive file object for the spreadsheet
    
    // Get the parent folder(s) of the spreadsheet. Assumes there is at least one parent folder.
    // If the spreadsheet is in the root of My Drive, this might require different handling.
    const parentFolders = spreadsheetFile.getParents(); // Gets an iterator for parent folders
    if (parentFolders.hasNext()) { // Checks if there is at least one parent folder
      const parentFolder = parentFolders.next(); // Gets the first parent folder
      DriveApp.getFileById(doc.getId()).moveTo(parentFolder); // Moves the newly created document to the parent folder
      Logger.log(`Document "${docTitle}" was moved to folder: ${parentFolder.getName()} (${parentFolder.getUrl()})`);
    } else {
      // If the spreadsheet has no parent folder (i.e., it's in My Drive root), the document stays in root.
      Logger.log(`Spreadsheet "${spreadsheet.getName()}" has no parent folder. The document remains in the root.`);
    }
  } catch (e) {
    Logger.log(`Error moving the document to the folder: ${e.message}`); // Logs any error during folder move
  }
  // ------------------------------------------------------------------

  // Get the URL of the created document and log it
  const docUrl = doc.getUrl();
  Logger.log(`Document was created: ${docUrl}`);

  // Display a notification to the user (will appear in the script editor or logs)
  SpreadsheetApp.getUi().alert('Document created', `A new document with a test link has been created. Check the logs for the document URL and its folder.`, SpreadsheetApp.getUi().ButtonSet.OK);
}

/**
 * Adds an option to the "Custom Menu" to run the test function.
 * This function is automatically called when the Google Sheet is opened.
 */
function onOpen() {
  SpreadsheetApp.getUi() // Gets the user interface object for the spreadsheet
    .createMenu('Custom Menu') // Creates a new custom menu named "Custom Menu"
    .addItem('Create Test Link Document', 'createDocumentWithTestLink') // Adds an item to the menu that calls createDocumentWithTestLink
    .addToUi(); // Adds the custom menu to the spreadsheet's UI
}