Skip to content

Instantly share code, notes, and snippets.

@jodmoreira
Created September 5, 2017 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jodmoreira/8ac2fef140e3fbdee0ad943449dabe65 to your computer and use it in GitHub Desktop.
Save jodmoreira/8ac2fef140e3fbdee0ad943449dabe65 to your computer and use it in GitHub Desktop.
Code to create a new row and update a list in google sheets
//Add this script to a google sheet that you will use as database. Set a trigger to run this script periodically. Daily midnight can be a good time
//Adicione esse escript à planilha do google que você vai usar como base de dados. Configure u acionador para funcionar periodicamente. Diariamente
//à meia noite pode ser um bom horário.
function updateList() {
//Don't forget to change the folder ID bellow
//Não se esqueça de mudar o ID da pasta abaixo
var folder = DriveApp.getFolderById('INSERT-HERE-THE-FOLDER-ID');
var contents = folder.getFiles();
var file;
var name;
var link;
var sheet = SpreadsheetApp.getActiveSheet();
var date;
var headers = [["Name", "URL", "Number of Sheets in Folder"]];
sheet.getRange("A1:C1").setValues(headers);
sheet.getRange("C2").setFormula("=COUNTA(A2:A)");
var sheetContent=[];
while(contents.hasNext()) {
file = contents.next();
name = file.getName();
link = file.getUrl();
data = [name, link]
sheetContent.push(data)
}
sheet.getRange(3,1,sheetContent.length,sheetContent[0].length).setValues(sheetContent);// adapt the starting row/column value to your needs
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// global
var ss = SpreadsheetApp.getActive();
function onOpen() {
var menu = [{name:"Add New Last Row", functionName:"addRow"}];
ss.addMenu("Extra", menu);
}
function addRow() {
var sh = ss.getActiveSheet(), lRow = sh.getLastRow();
//you will see belo the funcion that copy the last row and past in one row below.
var lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
//insert below, after que comma, inside parentheses, the number of row you want to create every time the script is activated
//coloque na próxima linha, após a vírgula, entre os parênteses, o número de linhas que você quer que sejam criadas
sh.insertRowsAfter(lRow, 2);
range.copyTo(sh.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
}
//Add this script to the same google sheet that you chose as database. Set a trigger to run it
function copyAndAddRow() {
//The funcion will use the active sheet and look for the last row. No visible action happens in this step.
//A função vai usar a planilha ativa e procurar pela última coluna. Nenhuma ação visível acontece nessa etapa.
var sh = ss.getActiveSheet(), lRow = sh.getLastRow();
//you will see bellow the funcion that copie the last row and pastes in one row right below
//abaixo você vai ver a função que copia a última linha e cola ela logo abaixo
var lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
range.copyTo(sh.getRange(lRow+1, 1, 1, lCol), {contentsOnly:false});
//insert below, after the comma, inside parentheses, the number of rows you want to create every time the script is activated. Here you will
//set the number of rows your report uses every time you import it.
//coloque na próxima linha, após a vírgula, entre os parênteses, o número de linhas que você quer que sejam criadas. Aqui você vai colocar a
//quantidade de colunas que o seu relatório usa toda vez que ele é importado.
sh.insertRowsAfter(lRow, 4);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment