104 lines
2.7 KiB
TypeScript
104 lines
2.7 KiB
TypeScript
export function getCellRangeByColumnName(
|
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
|
columnName: string,
|
|
row: number
|
|
) {
|
|
let data = sheet.getDataRange().getValues()
|
|
let column = data[0].indexOf(columnName)
|
|
if (column != -1) {
|
|
return sheet.getRange(row, column + 1, 1, 1)
|
|
}
|
|
}
|
|
|
|
export function getCellValueByColumnName(
|
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
|
columnName: string,
|
|
row: number
|
|
) {
|
|
let cell = getCellRangeByColumnName(sheet, columnName, row)
|
|
if (cell != null) {
|
|
return cell.getValue()
|
|
}
|
|
}
|
|
|
|
export function getColumnRangeByName(
|
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
|
columnName: string
|
|
) {
|
|
let column = getColumnByName(sheet, columnName)
|
|
if (column != -1) {
|
|
return sheet.getRange(2, column, sheet.getMaxRows())
|
|
}
|
|
}
|
|
|
|
export function getColumnByName(
|
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
|
columnName: string
|
|
) {
|
|
let data = sheet.getRange("A1:1").getValues()
|
|
let column = data[0].indexOf(columnName)
|
|
return column + 1
|
|
}
|
|
|
|
export function getColumnValuesByName(
|
|
sheet: GoogleAppsScript.Spreadsheet.Sheet,
|
|
columnName: string
|
|
) {
|
|
let column = getColumnRangeByName(sheet, columnName)
|
|
if (column != null) {
|
|
return column.getValues()
|
|
}
|
|
}
|
|
|
|
export function vlookupByColumns(
|
|
sheetName: string,
|
|
searchColumn: string,
|
|
searchKey: string,
|
|
resultColumn: string
|
|
) {
|
|
let s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
|
|
let searchData = getColumnValuesByName(s, searchColumn)
|
|
let dataList = searchData.map((x) => x[0])
|
|
let index = dataList.indexOf(searchKey)
|
|
|
|
if (index === -1) {
|
|
toastAndLog(searchKey + " not found")
|
|
return
|
|
}
|
|
let resultValue = getCellValueByColumnName(s, resultColumn, index + 2)
|
|
console.log("resultValue: " + resultValue)
|
|
return resultValue
|
|
}
|
|
|
|
export function toastAndLog(message: string) {
|
|
SpreadsheetApp.getActive().toast(message)
|
|
console.log(message)
|
|
}
|
|
|
|
export function getRowByColumnValue(
|
|
sheetName: string,
|
|
columnName: string,
|
|
searchKey: string
|
|
) {
|
|
let s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
|
|
let searchData = getColumnValuesByName(s, columnName)
|
|
let dataList = searchData.map((x) => x[0])
|
|
let index = dataList.indexOf(searchKey)
|
|
|
|
if (index === -1) {
|
|
toastAndLog(searchKey + " not found")
|
|
return
|
|
}
|
|
let resultRow = index + 2
|
|
console.log("row found: " + resultRow)
|
|
return resultRow
|
|
}
|
|
|
|
export function getColumnName(sheetName: string, col: number) {
|
|
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)
|
|
if (!sheet) {
|
|
return
|
|
}
|
|
let headers = sheet.getRange("A1:1").getValues()
|
|
return String(headers[0][col-1])
|
|
} |