I wasn't sure how to name title but here it goes
i need assistance to pass a variable when calling a function inside menu function
function onOpen(){
var ui = SpreadsheetApp.getUi();
ui.createMenu('Custom Menu')
.addSubMenu
(ui.createMenu('Save')
.addItem('Save All', 'doSaveAll')
.addSeparator()
.addItem('Save Grafics (Trade - Options - BTC - Termo - Future - Fund)', 'doSaveSheets')
.addSeparator()
.addItem('Save Trade', 'doSaveSheet("Trade")')
.addItem('Save Options', 'doSaveSheet("Opções")')
.addItem('Save BTC', 'doSaveSheet("BTC")')
.addItem('Save Termo', 'doSaveSheet("Termo")')
.addItem('Save Future', 'doSaveSheet("Future")')
.addItem('Save Fund', 'doSaveSheet("Fund")')
.addSeparator()
.addItem('Save Proventos','doSaveProventos')
.addSeparator()
.addItem('Save Balanço (BLC - DRE - FLC - DVA)','doSaveDatas')
//....
)
.addToUi();
};
when i try from menu to use Trade, i get error
Função de script não encontrada: doSaveSheet("Trade")
i guess it would be something like
Function script was not found: doSaveSheet("Trade")
is there a way from menu to call that function with that variale or do i need to workaround with something like:
function menuSaveTrade() {
doSaveSheet('Trade');
}
.addItem('Save Trade', 'menuSaveTrade()')
that would defeat the purpose of having function structured like this:
function doSaveSheets()
{
const sheetNames = ['Trade', 'Opções', 'BTC', 'Termo', 'Future', 'Fund'];
sheetNames.forEach(sheetName =>
{
try
{
doSaveSheet(sheetName);
}
catch (error)
{
console.error(`Error saving sheet ${sheetName}:`, error);
}
});
}
function doSaveSheet(sheetName)
{
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ss_t = ss.getSheetByName(sheetName); // ss_s = target spreadsheet
const ss_c = ss.getSheetByName('Config'); // ss_c = config spreadsheet
Utilities.sleep(2500); // 2,5 secs
console.log('Save:', sheetName);
if (sheetName === 'Trade')
{
var C2_ = ss_t.getRange('C2').getValue();
var Proventos = ss_c.getRange(ENP).getDisplayValue(); // ENP = Enable Proventos
var Save = ss_c.getRange(STR).getDisplayValue(); // STR = Save to Trade
const ss_p = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Prov');
var B3_ = ss_p.getRange('B3').getDisplayValue();
if( ( B3_ == "Proventos" || Proventos != "TRUE" ) && ( C2_ > 0 ) ) // check if error
{
processSaveSheet(ss_t, sheetName, Save)
doExportSheet(sheetName) // Special case to export alone
}
doCheck(sheetName)
}
//.......
}
those functions work, that isnt the problem, i need help just with the menu, cos i coulnt find a way to make it work, thanks in advance