简体   繁体   中英

Automatically Add a Note Based on Dynamic Cell in Google Sheets

I am trying to modify a Script in Google Sheets that creates a Note that contains the content of the Cell it lives in. I think I'm almost there- however, the script I have below only references a static cell. I need it to create a note within each cell in Column C, with each note referencing the text in the specific cell it is assigned.

For example:

  • C1 contains "TEST", C1 Note shows "TEST"
  • C2 contains "HELLO", C2 Note shows "HELLO"
  • C3 contains "WORLD", C3 Note shows "WORLD"

Here is the script that I have currently:

function addNote() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var targetCell = sheet.getRange("C3");
var sourceCell = sheet.getRange("C3");

var noteText = sourceCell.getValue();
targetCell.setNote(noteText);

}***

Appreciate any help that can be provided- also, would be great if this could update the content of the note when the spreadsheet is updated, if anyone knows how to append that.

  • You want to set the values of column "C" to the notes of the same column.
    • The values for the notes are the same with the values of cells of column "C".
    • For example, when the cell "C1" has "TEST", you want to set "TEST" to the note.
  • When the values of column "C" in 1st tab of active Spreadsheet is edited, you want to update the notes.
  • You want to achieve this using Google Apps Script.

From your question and your replying comments, I could understanding like above. If my understanding is correct, how about this sample script? Please think of this as just one of several answers.

In this sample script, I used a simple trigger. By this, when the column "C" of the 1st tab is edited, the script is run and the notes of column "C" are updated using the values of column "C".

Sample script:

Please copy and paste the following script and save the script. When you edit the cell of the column "C" in the 1st tab of active Spreadsheet, the script is run and the notes are set with the values of column "C".

function onEdit(e) {
  var sheet = e.source.getSheets()[0];
  if (e.range.getSheet().getSheetName() === sheet.getSheetName()) {
    var range = sheet.getRange("C1:C" + sheet.getLastRow());
    var values = range.getValues();
    range.setNotes(values);
  }
}

References:

The following script will create a note using the text from the cell that is being edited. The cell must be in the third column ("C"). To change the column that can be edited, change the 3 to the correct column number (A=1,B=2,C=3,etc...)

function onEdit(e){
  var range = e.range;
  if (range !== 3) return;
  range.setNote(e.value);
}

You may want to go a step further and clear the cell after creating the note. Here is an example of that:

function onEdit(e){
  var range = e.range;
  if (range.getColumn() !== 3) return;
  range.setNote(e.value);
  range.clearContent();
}

I have done something like this before and found it useful to be able to keep adding notes by entering values into the cell. Here's how you can do that:

function onEdit(e){
  var range = e.range;
  var newNote = '';
  var cellValue = e.value;
  if (range.getColumn() !== 3) return; 
   var previousNote = range.getNote() ? range.getNote() : '';
  if (previousNote) {
    newNote = cellValue + '\n\n' + previousNote;
  } else {
    newNote = cellValue;
  } 
  range.setNote(newNote);
  range.clearContent();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM