# Save Google Sheet in Google Cloud SQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
Gracias a Ruth González por ayudarme👍