Я учусь javascript и в настоящее время использую скрипт Google Apps как способ попрактиковаться и создать инструменты, которые я мог бы использовать для себя.
Я создал этот скрипт, чтобы запрашивать у пользователя ввод, флажок установлен и вставлен в ячейку, и если флажок снят, связанные ячейки будут очищены.
Я хочу развить хорошие навыки кодирования с самого начала и хотел бы, чтобы кто-нибудь просмотрел мой код и посмотрим, есть ли лучшее или более краткое решение.
/* When checkboxes are edited, user will be prompted to enter data, which will be filled in the correlating cells */
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//sets selected cell to currentCell
var currentCell = e.source.getCurrentCell();
//returns locations of currentCell
var rowIndex = Number(currentCell.getRow());
var columnIndex = Number(currentCell.getColumn());
//If edits aren't being made on Inventory Management, do not continue.
if (sheet.getName() !== "Inventory Management") return;
//Check if current cell is in column 10 aka Column J
if (Number(currentCell.getColumn()) == 10) {
var ui = SpreadsheetApp.getUi();
//Checks if action checks (True) or unchecks box (False)
if (currentCell.getValue().toString() == "true") {
var askingPrice = ui.prompt("Enter Asking Price ");
//If user clicks close, do not continue.
if (askingPrice.getSelectedButton() == ui.Button.CLOSE) {
return;
} else {
//Sets location for column 11 (K) and column 12 (L)
let priceColumn = columnIndex + 1;
let dateColumn = columnIndex + 2;
var datePosted = ui.prompt("Enter Date Posted (format: 'mm/dd/yyyy')");
//Sets the users responses to the correlating cells
sheet.getRange(rowIndex, priceColumn).setValue(Number(askingPrice.getResponseText()));
sheet.getRange(rowIndex, dateColumn).setValue(String(datePosted.getResponseText()));
}
//user unchecks box (false)
} else {
//Sets location for column 11 (K) and column 12 (L)
let priceColumn = columnIndex + 1;
let dateColumn = columnIndex + 2;
//clears values in correlating cells
sheet.getRange(rowIndex, priceColumn).setValue("");
sheet.getRange(rowIndex, dateColumn).setValue("");
}
}
//Check if current cell is in column 14 aka Column N
if (Number(currentCell.getColumn()) == 14) {
var ui = SpreadsheetApp.getUi();
//Checks if action checks (True) or unchecks box (False)
if (currentCell.getValue().toString() == "true") {
var soldPrice = ui.prompt("Enter Sold Price ");
//If user clicks close, do not continue.
if (soldPrice.getSelectedButton() == ui.Button.CLOSE) {
return;
} else {
//Sets location for column 14 (O), column 15 (P) and 17 (R)
let priceColumn = columnIndex + 1;
let dateColumn = columnIndex + 2;
let shipColumn = columnIndex + 4;
var dateSold = ui.prompt("Enter Date Sold (format: 'mm/dd/yyyy')");
var shipCost = ui.prompt("Enter Shipping Cost (if none, Enter 0)");
//Sets the users responses to the correlating cells
sheet.getRange(rowIndex, priceColumn).setValue(Number(soldPrice.getResponseText()));
sheet.getRange(rowIndex, dateColumn).setValue(String(dateSold.getResponseText()));
sheet.getRange(rowIndex, shipColumn).setValue(String(shipCost.getResponseText()));
}
//user unchecks box (false)
} else {
//Sets location for column 14 (O), column 15 (P) and 17 (R)
let priceColumn = columnIndex + 1;
let dateColumn = columnIndex + 2;
let shipColumn = columnIndex + 4;
//clears values in correlating cells
sheet.getRange(rowIndex, priceColumn).setValue("");
sheet.getRange(rowIndex, dateColumn).setValue("");
sheet.getRange(rowIndex, shipColumn).setValue("");
}
}
//Check if current cell is in column 22 aka Column V
if (Number(currentCell.getColumn()) == 22) {
var ui = SpreadsheetApp.getUi();
////Checks if action checks (True) or unchecks box (False)
if (currentCell.getValue().toString() == "true") {
var dateDonation = ui.prompt("Enter Date of Dontation");
//If user clicks close, do not continue.
if (dateDonation.getSelectedButton() == ui.Button.CLOSE) {
return;
} else {
//Sets location for column 23 (W)
let donationColumn = columnIndex + 1;
//Sets the users responses to the correlating cells
sheet.getRange(rowIndex, donationColumn).setValue(String(dateDonation.getResponseText()));
}
//user unchecks box (false)
} else {
//Sets location for column 23 (W)
let donationColumn = columnIndex + 1;
//clears values in correlating cells
sheet.getRange(rowIndex, donationColumn).setValue("");
}
}
}