Skip to content

Commit

Permalink
add lc022
Browse files Browse the repository at this point in the history
  • Loading branch information
ashtonfei committed Dec 27, 2023
1 parent 96a95e4 commit 1fb1062
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/projects/LC022/.clasp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"scriptId": "1El6EN0aTFJuQK6ddhnyvACOVb9kr3oe-ffoP2XtbvV--YNJ15SVjoDGG",
"rootDir": "./"
}
6 changes: 6 additions & 0 deletions src/projects/LC022/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeZone": "America/New_York",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
79 changes: 79 additions & 0 deletions src/projects/LC022/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @param {string} value The value to be checked
* @returns {boolean} true if it's valid formula
*/
const isFormula_ = (value) => /^=.*[)}"a-z0-9]+$/i.test(value);

/**
* @pram {string[]} headers The headers of the row values in the sheet
* @pram {objec} item The item object with headers as keys
* @pram {any[]} values The current values of the row
* @pram {string[]} formulas The current formulas of the row
* @returns {any[]} A list of values for the row
*/
const createRowValues_ = (headers) => (values, formulas) => (item) =>
headers.map((header, index) => {
const value = header in item ? item[header] : values?.[index];
const formula = formulas?.[index];
if (!formula) return value;
return isFormula_(value) ? value : formula;
});

/**
* @pram {string[]} headers The headers of the row values in the sheet
* @pram {objec[]} items The item objects with headers as keys
* @pram {any[][]} values The current values of the range
* @pram {string[][]} formulas The current formulas of the range
* @returns {any[][]} A list of values for the range
*/
const createRangeValues_ = (headers) => (values, formulas) => (items) =>
items.map((item, i) =>
createRowValues_(headers)(values?.[i], formulas?.[i])(item)
);

const test = () => {
const headers = ["name", "email", "age", "gender"];
const values = [
["Ashton", "", "", ""],
["Ella Zheng", "Link", "", ""],
];
const formulas = [
["", "=TODAY()", "", ""],
["", `=HYPERLINK(RC[-1];"Link")`, "", ""],
];
const createRangeValues = createRangeValues_(headers)(values, formulas);
const ashton = {
name: "Ashton Fei",
email: "=NOW()",
age: 30,
gender: "Male",
};
const ella = {
name: "Ella Fei",
email: "[email protected]",
age: 38,
gender: "Female",
};
const users = [ashton, ella];
const userValues = createRangeValues(users);
console.log(userValues);
};

const updateActiveRange = () => {
const title = "Update active range";
const ss = SpreadsheetApp.getActive();
const rangeList = ss.getActiveRangeList();
if (rangeList === null) return ss.toast("No selected ranges.", title);
const range = rangeList.getRanges()[0];
const [headers, ...values] = range.getValues();
const formulas = range.getFormulas().slice(0);
console.log(headers);
console.log(values);
console.log(formulas);
};

const onOpen = () => {
const menu = SpreadsheetApp.getUi().createMenu("LC022");
menu.addItem("Update active range", "updateActiveRange");
menu.addToUi();
};

0 comments on commit 1fb1062

Please sign in to comment.