I am attempting to compile a Database with financial information. To do this I need to import new data for each stock 'ticker' into the Database with a script that searches for the last empty row and pastes the data there.
However, If new data is available for a stock, I would like to paste it over the old in the Database - to avoid duplicates. And if there is no duplicates to continue with the original script to paste it at the next available row.
I think I would just need to tweak my current script to add a Find and Replace function coupled with an IF function.
Here is the script I have been working with so far:
//set spreadsheet and sheets
const spreadsheet = SpreadsheetApp.getActive();
const importerSheet = spreadsheet.getSheetByName('API ImporterTest');
const databaseSheet = spreadsheet.getSheetByName('DatabaseTest');
//cash flow
const lastRowColumnValuesCash = databaseSheet.getRange("G:G").getValues()
const lastRowIndexCash = lastRowColumnValuesCash.length - lastRowColumnValuesCash.reverse()
.findIndex(row => row[0] !== '');
//income statement
const lastRowColumnValuesIncome = databaseSheet.getRange("AT:AT").getValues()
const lastRowIndexIncome = lastRowColumnValuesIncome.length - lastRowColumnValuesIncome.reverse()
.findIndex(row => row[0] !== '');
//balance sheet
const lastRowColumnValuesBalance = databaseSheet.getRange("CC:CC").getValues()
const lastRowIndexBalance = lastRowColumnValuesBalance.length - lastRowColumnValuesBalance.reverse()
.findIndex(row => row[0] !== '');
//fundamentals
const lastRowColumnValuesFund = databaseSheet.getRange("DY:DY").getValues()
const lastRowIndexFund = lastRowColumnValuesFund.length - lastRowColumnValuesFund.reverse()
.findIndex(row => row[0] !== '');
//keymetrics
const lastRowColumnValuesMet = databaseSheet.getRange("GE:GE").getValues()
const lastRowIndexMet = lastRowColumnValuesMet.length - lastRowColumnValuesMet.reverse()
.findIndex(row => row[0] !== '');
//profile
const lastRowColumnValuesProf = databaseSheet.getRange("IN:IN").getValues()
const lastRowIndexProf = lastRowColumnValuesProf.length - lastRowColumnValuesProf.reverse()
.findIndex(row => row[0] !== '');
//run
if (databaseSheet.getLastRow() + 15 > databaseSheet.getMaxRows())
databaseSheet.insertRowsAfter(databaseSheet.getMaxRows(),
databaseSheet.getLastRow() + 15 - databaseSheet.getMaxRows()
)
importerSheet
.getRange('F3:J40')
.copyTo(
databaseSheet.getRange(`G${lastRowIndexCash + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
importerSheet
.getRange('S3:W36')
.copyTo(
databaseSheet.getRange(`AT${lastRowIndexIncome + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
importerSheet
.getRange('AF3:AJ49')
.copyTo(
databaseSheet.getRange(`CC${lastRowIndexBalance + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
importerSheet
.getRange('AS3:AW59')
.copyTo(
databaseSheet.getRange(`DY${lastRowIndexFund + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
importerSheet
.getRange('BF3:BJ62')
.copyTo(
databaseSheet.getRange(`GE${lastRowIndexMet + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
importerSheet
.getRange('BS3:BW36')
.copyTo(
databaseSheet.getRange(`IN${lastRowIndexProf + 1}`),
SpreadsheetApp.CopyPasteType.PASTE_VALUES,
true
);
};
Here is a functional Test Sheet to test it on with example solutions (to produce a new dataset to test, change the cell 'C2' in 'API Importer Test' to another ticker, FB, AAPL, GME for example and hit import.)
https://docs.google.com/spreadsheets/d/1UPAz7ku_AILB9aDvKCVyClvBh_5XskGPgGh0w784PR4/edit?usp=sharing
Aucun commentaire:
Enregistrer un commentaire