Vediamo come costruire una funzione che ci permetta di creare da apps script un nuovo foglio su spreadsheet ed aggiungerlo a quelli esistenti.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
function aggiungiFoglio(nomeFoglio) { var cartella = SpreadsheetApp.getActiveSpreadsheet(); var nullaosta = false; var tmp = nomeFoglio; var trovati = 0; while( true ) { nullaosta = true; cartella.getSheets().forEach(function(f) { if( f.getName() == tmp ) { tmp = nomeFoglio + " (" + (++trovati) + ")"; nullaosta = false; } }); if( nullaosta ) break; } cartella.insertSheet(tmp); return SpreadsheetApp.getActiveSpreadsheet().getSheetByName( tmp ); } |
Notiamo che per prendere il riferimento alla cartella esistente utilizziamo:
|
|
var cartella = SpreadsheetApp.getActiveSpreadsheet(); |
Il nome del foglio lo vogliamo gestire nel modo seguente: se il nome che stiamo cercando…