Using Apps Script to Add a New Row and Insert Hyperlinks in Google Sheets

My code appends a row for every edit made on a spreadsheet, including the following details:

Date, Time, Sheet Name, Cell Location, User

The original code is as follows:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();


  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"),
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,
  sheet.getActiveCell().getA1Notation(),
  Session.getActiveUser().getEmail()]);

}

My goal is to hyperlink the 'Cell Location' part to the original location using this function:

sheet.getRange(1,1)
.setValue('=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
+'/edit#gid='
+ss.getActiveSheet().getSheetId()
+'&range='
+sheet.getActiveCell().getA1Notation()+'")');

I attempted to modify the code as such:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();

  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"),
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,

  .setValue('=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
   +'/edit#gid='
   +ss.getActiveSheet().getSheetId()
   +'&range='
   +sheet.getActiveCell().getA1Notation()+'")'),

  Session.getActiveUser().getEmail()]);

}

Unfortunately, this modification did not work. Any suggestions or advice would be greatly appreciated.

I suspect the issue may be due to starting the line with .setValue(), but I'm unsure.

Thank you.

Answer №1

When making your final revisions to the code snippet, it is important to note that you did not specify a range before calling the .setValue() method. In this specific scenario, using .setValue() may actually be unnecessary since the value being appended already exists within the row. Consider the following adjusted approach:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();

  var link = '=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
  +'/edit#gid='+ss.getActiveSheet().getSheetId()+'&range='
  +sheet.getActiveCell().getA1Notation()+'")';

  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"), 
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, 
  SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,link,Session.getActiveUser().getEmail()]);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What methods can be used to transmit binary information from a Node.js socket.io server to a web browser?

After extensively searching the Socket.IO documentation, I am still unable to locate a straightforward example of how to send binary data between a server and client. Despite their assurances that it is included, I have yet to find a minimal demonstration. ...

Tips for activating a function right before a session timeout in ASP.NET

I'm currently working on implementing a feature that tracks user activity by recording check-ins and checkouts before their session expires. I have set up a session timeout to handle this but now I need to call a method using JavaScript and Ajax to ru ...

Barba.js Revolutionizes Page Reloading for Initial Links

On my Wordpress site, I am using Barba js v.2 for page transitions. However, I have encountered an issue where I need to click on a link twice in order to successfully change the page and make the transition work. Interestingly, the first time I click on t ...

jQuery - delete a single word that is case-sensitive

Is there a way to eliminate a specific case-sensitive word from a fully loaded webpage? The word in question is "Posts" and it appears within a div called #pd_top_rated_holder that is generated by Javascript. The Javascript code is sourced externally, so ...

Developing an array-based multiple choice quiz

Being a complete novice, I am currently immersed in designing a multiple-choice quiz using arrays. The questions all follow a similar template: "Which word in the following has a similar meaning to '_________'." To implement this, I have created ...

Using JQuery, you can toggle a newly created DIV element by linking it to `$(this)` instead of `$(this).closest()`

In the comment section, there is a link called "Reply" that triggers a pop-up comment box when clicked. However, I want the comment box to disappear if the "reply" button is clicked again, as it currently keeps opening more comment boxes. $('.replyli ...

Tips for preventing the colors of all buttons from changing when only one is clicked in JavaScript

const tasks = `[{ "taskName": "Task 1", "image": "./images/task1.jpg", "description": "Task 1 description", "importance": 0 }, { "taskName": "Task 2", "image": "./images/task2.jpg", "description": "Task 2 description", ...

Adjusting the heights of all elements with identical ids simultaneously

I found the following code: <div xmlns="http://www.w3.org/1999/xhtml" style="z-index: 1; position: absolute; top: 90px; left: 90px; background: none repeat scroll 0% 0% black; width: 800px;" id="mainDiv"> <div style="width: 405px;" class= ...

"Enhanced file manager: Elfinder with multiple buttons to seamlessly update text input fields

Every button is responsible for updating the respective element: <input type="text" id="field" name="image" value="<?php echo @$DuzenleSonuc[0]['image']; ?>" /> I need to ensure that each button updates the correct field: onclick ...

How can I maintain the hover color on a button with a text label even when hovering over the text label?

I need help creating a button with a text label that both respond to clicks and have a hover color. The current code I have works, but the hover color is lost when hovering over the text label. How can I modify the code to maintain the hover color even w ...

Transmit a message from the background.js script to the popup window

Currently, I am looking to integrate FCM (Firebase Cloud Messaging) into my chrome extension. After conducting thorough research, I have discovered that the most efficient way to implement FCM is by utilizing the old API chrome.gcm. So far, this method has ...

What is the method for sending arguments to material avatar using require?

import Avatar from '@material-ui/core/Avatar'; Here is an example of working code: <Avatar alt="user 4" src={require('Assets/img/user-1.jpg')} className="size-80 rounded-circle border-info rct-notify" /> However, I encountered ...

Obtaining form data from connected drop-down menus and sending it to PHP

My webpage contains a form with 2 dependent drop-down lists. When a user selects a value from the first list, it populates the second list for the user to make another selection. However, when I try to submit the form data to a PHP page to insert into a M ...

The error message states: "An error occurred: Unable to find the 'HttpProvider' property within an undefined

import * as Web3 from 'web3' import { OpenSeaPort, Network } from 'opensea-js' const provider = new Web3.providers.HttpProvider('https://mainnet.infura.io') Hello everyone, I'm currently learning Node.js and encountered ...

Retrieving attribute values when using the .on function in jQuery

I currently have 10 links with the following format: <a href="#" data-test="test" class="testclass"></a> as well as a function that looks like this: $(document).on("click", ".testclass", function () { alert($(this).attr('data-t ...

Screening strings and arrays based on multiple criteria

My code is below and I am trying to have the bot check for two specific conditions in the user's message. The message must contain "how" plus either "doing" or "bread". It works perfectly when using only "doing" but not when adding the "bread" conditi ...

Utilize AngularFire to generate a new User and invoke the factory function to showcase notifications

I recently started working with AngularJS and wanted to integrate it with AngularFire/Firebase for storing project data. I created a factory that manages notifications to be displayed in a centralized location. Here is the structure of my factory: myApp.f ...

button that takes you back to the top of a mobile website

I want to customize the Scroll To Top button so that it only appears once the user begins scrolling down, rather than always being visible even when at the top of the page. Just a heads up, I don't have much experience with JavaScript, so I might need ...

GraphQL Error (Status Code: 429) - Next.js Development issue

If you're facing a GraphQL Error (Code: 429) while working on a nextjs project, here's a handy solution. Here's what happened: I created a headless CMS using Hygraph and NextJS 13 for a blog project. I also utilized the npm package graphql ...

What methods can I use to prevent multiple calls to isValid in this particular jQuery validation scenario?

I am currently working on validating a field with the following requirements: No validation when the user first lands on the page Validation triggers when the user clicks on the Name Query field, and it validates on both key up and focus out events The f ...