Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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

This comment has been minimized.

Copy link
Owner Author

@seoutopico 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
You can’t perform that action at this time.