I am creating a script to automatically add data to the first sheet/tab of a spreadsheet. I need to do this through a script.google.com as there are various spreadsheets and I want to keep this code in a single script. Also, the name of the first tab changes every week.
I can add data to cells if I know the sheets name using range: "Sheet1!A2". However I want to do this without knowing "Sheet1". Something like newestSheet+"!A2".
function myFunction(){
updateGoogleSheet("My Sheet ID");
}
function updateGoogleSheet(spreadsheetId) {
var newestSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getName();
var data = [
{
range: newestSheet+"!A2", // Update single cell
values: [
["2"]
]
},
];
var resource = {
valueInputOption: "USER_ENTERED",
data: data
};
Sheets.Spreadsheets.Values.batchUpdate(resource, spreadsheetId);
}
SpreadsheetApp doesn't work with script.google.com. Is there an alternative to "getSheets()[0]" to return the name of the first sheet?
getActive()
. TryopenById()
– Cooperrange: newestSheet+"!A2"
torange: "A2"
. And remove the line ofvar newestSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getName()
. – Tanaike