Ответьте на вопрос: Планирование панели мониторинга продаж в листах Google
Я написал скрипт в скрипте приложений Google, который решил проблему назначения каналов на основе общей цели продаж.
Сценарий, который я написал, работает, но он жестко закодирован. Я надеялся на некоторую помощь в ее оптимизации - кто-нибудь может мне помочь с этим?
Сценарий:
function calculate() {
// Get the planning sheet
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
var sheet = spreadsheet.getSheetByName('Planner')
// Get contract target as the input
var target = sheet.getRange('B1').getValue()
// Clear the pin cards list
sheet.getRange('D11:D15').clearContent()
// Set the value of the first channel
firstChannelValue(sheet, target)
// Set the value of the second channel
secondChannelValue(sheet, target)
// Set the value of the third channel
thirdChannelValue(sheet, target)
// Set the value of the fourth channel
fourthChannelValue(sheet, target)
// Set the value of the fifth channel
fifthChannelValue(sheet, target)
}
function firstChannelValue(sheet, target) {
// Max number of pin cards
var pcLimit = sheet.getRange('E11').getValue()
// Conversion rate
var convRate = sheet.getRange('F11').getValue()
// Cell that needs to be filled
var pinCard = sheet.getRange('D11')
// Max possible value
var maxValue = pcLimit*convRate
// Set value of cell
if (maxValue <= target) {
pinCard.setValue(pcLimit)
}
else {
pinCard.setValue(target/convRate)
}
}
function secondChannelValue(sheet, target) {
// Max number of pin cards
var pcLimit = sheet.getRange('E12').getValue()
// Conversion rate
var convRate = sheet.getRange('F12').getValue()
// Cell that needs to be filled
var pinCard = sheet.getRange('D12')
// Max possible value
var maxValue = (pcLimit*convRate)
// Value of first channel contracts
var firstChannel = sheet.getRange('G11').getValue()
// Revised target
var revisedTarget = target - firstChannel
// Set value of cell
if (maxValue <= revisedTarget) {
pinCard.setValue(pcLimit)
}
else {
pinCard.setValue(revisedTarget/convRate)
}
}
function thirdChannelValue(sheet, target) {
// Max number of pin cards
var pcLimit = sheet.getRange('E13').getValue()
// Conversion rate
var convRate = sheet.getRange('F13').getValue()
// Cell that needs to be filled
var pinCard = sheet.getRange('D13')
// Max possible value
var maxValue = (pcLimit*convRate)
// Value of previous channel contracts
var firstChannel = sheet.getRange('G11').getValue()
var secondChannel = sheet.getRange('G12').getValue()
// Revised target
var revisedTarget = target - firstChannel - secondChannel
// Set value of cell
if (maxValue <= revisedTarget) {
pinCard.setValue(pcLimit)
}
else {
pinCard.setValue(revisedTarget/convRate)
}
}
function fourthChannelValue(sheet, target) {
// Max number of pin cards
var pcLimit = sheet.getRange('E14').getValue()
// Conversion rate
var convRate = sheet.getRange('F14').getValue()
// Cell that needs to be filled
var pinCard = sheet.getRange('D14')
// Max possible value
var maxValue = (pcLimit*convRate)
// Value of previous channel contracts
var firstChannel = sheet.getRange('G11').getValue()
var secondChannel = sheet.getRange('G12').getValue()
var thirdChannel = sheet.getRange('G13').getValue()
// Revised target
var revisedTarget = target - firstChannel - secondChannel - thirdChannel
// Set value of cell
if (maxValue <= revisedTarget) {
pinCard.setValue(pcLimit)
}
else {
pinCard.setValue(revisedTarget/convRate)