I have a Google Spreadsheet as shown below.
The 30 at the bottom is what the title takes to display the points. How do I make it so that when the user clicks the buy button it will deduct the amount of points next to it (ignoring the text) from the 30 at the bottom and add a line below the 30 with it saying the item that was just purchased?
EX: User clicks the "Buy" Button on Item 1. Code takes the numbers only from the previous cell and deducts it from the 30 at the bottom of the sheet then creates a new row under the cell that was 30 and the new line's first cell displays the "Item"
Reference Image:
If anyone could help me with this that would be great... Thanks!
Update: I have it working now, But how can I make it so that instead of deleting the previous purchases it adds it to an unused line?
Here is the code so far:
function onSelectionChange(e) {
const sheet = SpreadsheetApp.getActiveSheet();
const resultA1Notation = "A21"; // Change according to your preferences
const itemNotation = "A22"; // Change according to your preferences
const range = e.range;
const selectedValue = range.getValue();
if (selectedValue === "Buy") {
let numberValue = range.offset(0, -1).getValue();
const itemValue = range.offset(0, -4).getValue();
if (numberValue !== "Free") {
numberValue = numberValue.slice(0, -1); // Remove P
const resultCell = sheet.getRange(resultA1Notation);
resultCell.setValue(resultCell.getValue() - numberValue);
}
sheet.getRange(itemNotation).setValue(itemValue + " was purchased!");
}
}
Thanks!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…