Skip to content

Instantly share code, notes, and snippets.

@seoutopico
Last active May 9, 2018 07:50
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 seoutopico/dd01d8c739148232fe1af5107114a9da to your computer and use it in GitHub Desktop.
Save seoutopico/dd01d8c739148232fe1af5107114a9da to your computer and use it in GitHub Desktop.
# Save Google Sheet in Google Cloud SQL
// Replace the variables in this block with real values.
// You can find the "Instance connection name" in the Google Cloud
// Platform Console, on the instance Overview page.
var connectionName = 'xxx';
var user = 'xxx';
var userPwd = 'xxx';
var db = 'xxx';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
function writeOneRecord() {
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
conn.setAutoCommit(false);
var start = new Date();
stmt = conn.prepareStatement('insert into posiciones4 '
+ '(nombre,palabra) values (?,?)');
var sheet = SpreadsheetApp.getActiveSheet();
var nombre = sheet.getRange('A2:A').getValues();
var palabra = sheet.getRange('B2:B').getValues();
for (var row = 0, len = nombre.length; row < len; ++row) {
if (nombre[row] != '') {
stmt.setString(1, nombre[row]);
stmt.setString(2, palabra[row]);
stmt.addBatch();
}
}
var batch = stmt.executeBatch();
conn.commit();
conn.close();
var end = new Date();
Logger.log('Time elapsed: %sms for %s rows.', end - start, batch.length);
}
//Menú para lanzar el script
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('NombreDelmenu')
.addItem('Guardar', 'writeOneRecord')
.addToUi();
}
@seoutopico
Copy link
Author

seoutopico commented May 9, 2018

Save Google Sheet in Google Cloud SQL

Script que te permite guardar la hoja activa del Google sheet en la base de datos de Google Cloud SQL

Añado un menu para lanzar el script

menu

Gracias a Ruth González por ayudarme 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment