What I’m trying to do
I am trying to create a running balance sheet to manage finances. I don’t want to use the ledger or hledger plugins. I have everything as I want it EXCEPT, I can’t seem to convert expenses to negative numbers so I can calculate easily. I want to convert a numeric value from a modal form to negative when the input indicates it’s an expense, and populate the frontmatter with the new, negative value.
I have a Transaction form that collects transactional data (both income and expenses). There are two toggle buttons. One indicating it’s income (positive) and the other indicates expenses (negative). The user inputs positive numbers into the form.
If the transaction is an expense, I want it to convert the user inputted value into a negative number. For example, on the form, I am entering an expense for gas that’s 45.57. I want to use script to recognize that the expense toggle is set to true, and change that value to -45.57 before entering the value into the frontmatter.
Templater is being used to create the note.
Things I have tried
Here is my templater script:
<% “—” %>
<%*
const modalForm = app.plugins.plugins.modalforms.api;
const result = await modalForm.openForm(“transactions”);
const data = result.getData();
const value = data.value;
const is_negative = data.is_negative;
let finalValue = value;
if (is_negative) {
finalValue = value * -1;
}
tR += result.asFrontmatterString();
app.fileManager.processFrontMatter(tp.config.target_file, frontmatter => {
frontmatter['value'] = finalValue;
});
});
-%>
<% “—” %>
DATE: <% result.get(“date_transaction”) %>
BANK: <% result.get(“bank_name”) %>
INCOME?: <% result.get(“is_positive”) %>
INCOME CATEGORY: <% result.get(“income_category”) %>
EXPENSE?: <% result.get(“is_negative”) %>
EXPENSE CATEGORY: <% result.get(“expense_category”) %>
DESCRIPTION: <% result.get(“transaction_name”) %>
AMOUNT: <% result.get(“value”) %>
<% await tp.file.rename(result.get(“date_transaction”)+" “+result.get(“transaction_name”)+” ")%>
The form works, the note is created with all of the frontmatter fields that are needed. The only problem is, it doesn’t change the number value to negative.
Any thoughts? I’m sure it’s a simple answer that I’m just missing.
I tried searching:
- Modal forms change numbers to negative conditional
- Templater change numbers to negative
- dataviewjs if-else
- dataview choice