Google's Document Automation Script

This is my first venture into the world of Google Scripts, and I must say, it's quite exciting!

After some searching and tweaking, I managed to get a Sheets function up and running. It's designed to send an email notification about a status update on a specific row, triggered by a menu selection. However, I'd like to take it a step further by having it also append text to a column labeled "Email Sent" (which happens to be column O).

Below is the current script:

function getColIndexByName(colName) {
  var sheet = SpreadsheetApp.getActiveSheet();
  var numColumns = sheet.getLastColumn();
  var row = sheet.getRange(1, 1, 1, numColumns).getValues();
  for (i in row[0]) {
    var name = row[0][i];
    if (name == colName) {
      return parseInt(i) + 1;
    }
  }
  return -1;
}

function emailStatusUpdates() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = sheet.getActiveRange().getRowIndex();
  var userEmail = sheet.getRange(row, getColIndexByName("email")).getValue();
  var subject = "Helpdesk Ticket #" + row;
  var body = "We've updated the status of your ticket.\n\nStatus: " + sheet.getRange(row, getColIndexByName("Status")).getValue();
  body += "\n\nNotes: " + sheet.getRange(row, getColIndexByName("Notes")).getValue();
  body += "\n\nResolution: " + sheet.getRange(row, getColIndexByName("Resolution")).getValue();

  MailApp.sendEmail(userEmail, subject, body, {name:"Help Desk"});
}

function onOpen() {
  var subMenus = [{name:"Send Status Email", functionName: "emailStatusUpdates"}];
  SpreadsheetApp.getActiveSpreadsheet().addMenu("Help Desk Menu", subMenus);
}​

Answer №1

It's actually quite simple. All you need to do is utilize the setValue method within the emailStatusUpdates function.

var emailConfirmation = "Email successfully sent at " +
  Utilities.formatDate(new Date(), Session.getScriptTimeZone(),
                       "yyyy-MM-dd HH:mm:ss");
sheet.getRange(row, getColIndexByName("email date")).setValue(emailConfirmation);

To avoid hard-coding column "O" in the script, I'm implementing your function getColIndexByName. Just ensure that the header for column O matches what is being passed to this function.

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

Wait for the completion of a Promise inside a for-loop in Javascript before returning the response

After completing a Promise within a for-loop, I am attempting to formulate a response. Although I have reviewed these questions, my scenario remains unaddressed. The methodGetOrders and methodGetLines are components of an external library that must be ut ...

Choosing a specific date from an input field of type 'month'

Given: <input class="inputField_input__2B0dA" maxlength="7" id="establishedYear" data-testid="date-input" type="month" value=""> This input field on the page resembles a date format: ----- ...

An innovative way to display or hide a div depending on the multiple selections made in a select2 jQuery field

A select2 jQuery dropdown menu is set up with two options: *For Rent *For Sales If the user selects 'For Rent', specific fields will be displayed below it, as well as for 'For Sales'. The challenge arises when both options are ...

What is the best way to generate separate instances of a React element for different copies?

Just started learning React. I'm working on creating a set of 12 range sliders, arranged in two columns of six each. Each column is defined separately and has its own instance of the slider. The react-slider package is used for each range slider, wit ...

Calculating the position of an element in an array passed from a separate file with ReactJS and Ant Design

I'm currently working with a file that contains some JavaScript code featuring an array with multiple objects containing key-value pairs. In my main file (App.jsx), I initialize a State Variable and assign it the array from another JS file. My goal no ...

Next.js Head component will not repeat the same Meta Tags

In my Next.js project, I have implemented different meta tags with various media targets in the Head section: <Head> <meta name="theme-color" media="(prefers-color-scheme: light)" content="#7f8fa6"/> <meta name= ...

Beware, search for DomNode!

I attempted to create a select menu using material-ui and React const SelectLevelButton = forwardRef((props, ref) => { const [stateLevel, setStateLevel] = useState({ level: "Easy" }); const [stateMenu, setStateMenu] = useState({ isOpen ...

What is the best way to preserve the state of a component in Angular?

I need to find a way to preserve the state of a data table within my Angular 7 & Typescript application. It's essentially like saving the state of a browser tab. When I navigate to another component and return to the one with the data table, how ...

Upon attempting to open Google Maps for the second time, an error message pops up indicating that the Google Maps JavaScript API has been included multiple times on this page

Currently, I am utilizing the npm package known as google-maps and integrating it with an angular material modal to display a map. However, upon opening the map for the second time, an error message is triggered: You have included the Google Maps JavaScri ...

Is there a way to ensure my fetch request is only triggered once within a React Component?

I am trying to fetch an object and extract a URL from it, but for some reason my console.log is being repeated 8 times. Can anyone spot where I might be going wrong? import React from 'react'; // import Card from './Card'; function Gam ...

Is there a method in JavaScript/jQuery to gently push or flick the scroll so that it can be easily interrupted by the user? Further details are provided below

I am looking for a solution in javascript that will allow the page to automatically scroll down slightly, but I also want the user to be able to interrupt this scroll. When using jquery for auto-scrolling, if the user begins manual scrolling while the ani ...

When using Phonegap or Mobile Safari, the jQuery $.get function does not function properly with "local" files

I've come across an issue with my Phonegap App's templating system that utilizes jQuery and the $.get() function. $.get('templates/' + name + '.html', someFunctionHere(){}); While the templating system works perfectly when I ...

Eliminate the excess padding from the Material UI textbox

I've been struggling to eliminate the padding from a textbox, but I'm facing an issue with Material UI. Even after setting padding to 0 for all classes, the padding persists. Could someone provide guidance on how to successfully remove this pad ...

Have you ever wondered why a listener on the 'data' event interprets two separate requests as one, unless a timeout is added?

It seems that there is a tricky issue with node where it combines 2 different requests into one unless a timeout is added. In the code snippet below, if we write 'one' and 'two' to the server and push the result to an array, node interp ...

CKEditor with Readonly Option and Active Toolbar Controls

In my current Rails project, I have successfully set up a CKEditor instance in readOnly mode. Everything is functioning as expected. Now, I am attempting to add a custom plugin button to the toolbar while keeping the textarea in readOnly mode. After some ...

using javascript to invoke php function

I encountered an issue when attempting to call a PHP function from JavaScript. The code I used is displayed below: <html> <head></head> <body> <script type="text/javascript" > function header() { <?php ...

Implement a sleek carousel within a Vue component

Hello, I am trying to add a single slick carousel component to a block that already contains 2 components in a template. However, when I do this, it distorts the images of the other component. <template v-if="isMobile"> <vue-slic ...

Is there a way to extract a username from LDAP?

Can you help me understand how to dynamically retrieve a username from LDAP? In the code snippet below, I have hardcoded the username as 'smith2': $_SERVER["REMOTE_USER"] = 'smith2'; $param = $_SERVER["REMOTE_USER"] By using this appr ...

What are some reasons that event.preventDefault() may not be effective in certain situations?

I'm experiencing an issue with submitting a form via ajax. I am trying to validate the input field values before submission, but even if the criteria are not met, the form still submits. I have checked my code and the form submission function returns ...

Troubleshooting: Bootstrap accordion malfunctioning when using a numerical ID

Issues have arisen after updating Bootstrap to version 4.2.1. In this latest version, using id="#12" in an accordion does not seem to work properly. However, in previous versions, it worked just fine with the same id="#12". Any suggestions on how to resolv ...