简体   繁体   中英

Google Sheets add a note by appending column based on matching value

The goal is to add a note or several notes over time to a google sheet based on the value chosen.

The script will allow for a button to be added in the sheet and run the script function to append to the next available column.

For example, the name Tim is chosen. A note is written, the button is pressed to run the function and it will add it to Column D (since it is the next available column for Tim).

Another example, the name Jeff is chosen. A note is written. Since there are no more columns the append column should automatically create a new column and allow for the note to be written in "Jeff's" row, which would be Column G

Not sure if this is at all possible but hope to get some suggestions or ideas.

A B C D E F
Mark B+ completed assignment partly but needed extra time since.. 89433 other
Tim A checked
Jeff C n/a assignment # 4 done other
Steve A completed get file received
Elon B- three out of four check email

Here's what I think you're trying to achieve -

添加注释

And, this code should help you get started -

function main() {
  const name = "Steve";
  const note = "HELLO"
  addNote(name, note);
}

function addNote(name, note) {
  const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const values = ss.getDataRange().getValues();
  for (var rowIndex = 0; rowIndex < values.length; rowIndex++) {
    let row = values[rowIndex];
    let nameColumn = row[0];
    if (nameColumn == name) {
      let rowValues = ss.getRange(`${rowIndex+1}:${rowIndex+1}`).getValues()[0]
      .filter(value => value !== "")
      let newColIndex = rowValues.length;
      ss.getRange(rowIndex+1, newColIndex+1).setValue(note);
    }
  }
}

Although, I'd highly recommend choosing something else as a unique identifier as opposed to a name because if there are 2 rows with the same name, then the code would actually add notes against both (incorrectly).

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