I am attempting to compile a Database with financial information. To do this I need to import new data for each stock 'ticker' from an External API using JSON. I have been using a really good script to do this so far, which has worked in copying, transposing, and pasting the new data in the next available rows.
However, I am now facing problems with multiples. If new data is available for a stock, I would like to paste it over the old in the Database, and if there is no duplicates to continue with the original script to paste it at the next available row.
I think I would need an advanced Find and Replace function and maybe an IF function. I am not the best at scripting so I thought I would ask the community.
Here is the script I have been working with so far: (No code has been added to attempt any find and replace functions)
//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 the sheet which is open to editing <*https://docs.google.com/spreadsheets/d/1UPAz7ku_AILB9aDvKCVyClvBh_5XskGPgGh0w784PR4/edit?usp=sharing*>
Aucun commentaire:
Enregistrer un commentaire