usar texto sheets que porcentaje numero importar hoja google funcion datos convertir como caracteristicas calculo google-sheets gdata-api google-spreadsheet-api gdata-python-client

google sheets - texto - ¿Cómo convertir el ID de cadena de la hoja de cálculo de Google al índice entero(GID)?



porcentaje google sheets (6)

Encontré su pregunta buscando una solución al mismo problema, y ​​me sorprendió que las ID de la hoja de trabajo realmente correspondieran 1: 1 a las gids ; originalmente asumí que se asignaban de forma independiente, en lugar de ser un ejercicio de ofuscación.

Pude encontrar una solución un poco más limpia mediante la ingeniería inversa de la fórmula que usan para generar los ID de las hojas de trabajo de su tabla:

worksheetID = (gid xor 31578) encoded in base 36

Entonces, algunos Python para pasar de un ID de hoja de trabajo a gid :

def to_gid(worksheet_id): return int(worksheet_id, 36) ^ 31578

Esto todavía está sucio, pero funcionará para GIDs superiores a 99 sin requerir tablas gigantes. Al menos siempre y cuando no cambien la lógica de generación (lo cual probablemente no lo harán, ya que rompería las identificaciones existentes que las personas ya usan).

Para exportar la hoja de trabajo única de la hoja de cálculo de Google a CSV, se requiere que se pase el índice de hoja de trabajo (GID).

https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&gid=%d&exportFormat=csv

Pero, ¿dónde están esas informaciones? Con gdata.spreadsheets.client, pude encontrar una ID de cadena para la hoja de trabajo como "oc6, ocv, odf".

client = gdata.spreadsheets.client.SpreadsheetsClient() feed = client.GetWorksheets(spreadsheet, auth_token=auth_token)

Y devuelve debajo del atom XML. (Parte de ello)

<entry gd:etag="&quot;URJFCB1NQSt7ImBoXhU.&quot;"> <id>https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw</id> <updated>2012-06-21T08:19:46.587Z</updated> <app:edited xmlns:app="http://www.w3.org/2007/app">2012-06-21T08:19:46.587Z</app:edited> <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#worksheet"/> <title>AchievementType</title> <content type="application/atom+xml;type=feed" src="https://spreadsheets.google.com/feeds/list/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw/private/full"/> <link rel="http://schemas.google.com/spreadsheets/2006#cellsfeed" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/ocw/private/full"/> <link rel="http://schemas.google.com/visualization/2008#visualizationApi" type="application/atom+xml" href="https://spreadsheets.google.com/tq?key=0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c&amp;sheet=ocw"/> <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/private/full/ocw"/> <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/0AvhN_YU3r5e9dGpTWGx3UVU3MTczaXJuNEFKQjMwN2c/private/full/ocw"/> <gs:rowCount>280</gs:rowCount> <gs:colCount>28</gs:colCount> </entry>

También probé con el parámetro de la hoja pero fallé con el error "Hoja no válida".

https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&sheet=XXX&exportFormat=csv

Supongo que debería haber alguna función mágica pero no podría encontrarla. ¿Cómo puedo convertirlos a ID entero? O ¿Puedo exportar la hoja de trabajo con el id de cadena?

EDITAR: Acabo de hacer convertir tabla con python. Sucio pero trabajando :-(

GID_TABLE = { ''od6'': 0, ''od7'': 1, ''od4'': 2, ''od5'': 3, ''oda'': 4, ''odb'': 5, ''od8'': 6, ''od9'': 7, ''ocy'': 8, ''ocz'': 9, ''ocw'': 10, ''ocx'': 11, ''od2'': 12, ''od3'': 13, ''od0'': 14, ''od1'': 15, ''ocq'': 16, ''ocr'': 17, ''oco'': 18, ''ocp'': 19, ''ocu'': 20, ''ocv'': 21, ''ocs'': 22, ''oct'': 23, ''oci'': 24, ''ocj'': 25, ''ocg'': 26, ''och'': 27, ''ocm'': 28, ''ocn'': 29, ''ock'': 30, ''ocl'': 31, ''oe2'': 32, ''oe3'': 33, ''oe0'': 34, ''oe1'': 35, ''oe6'': 36, ''oe7'': 37, ''oe4'': 38, ''oe5'': 39, ''odu'': 40, ''odv'': 41, ''ods'': 42, ''odt'': 43, ''ody'': 44, ''odz'': 45, ''odw'': 46, ''odx'': 47, ''odm'': 48, ''odn'': 49, ''odk'': 50, ''odl'': 51, ''odq'': 52, ''odr'': 53, ''odo'': 54, ''odp'': 55, ''ode'': 56, ''odf'': 57, ''odc'': 58, ''odd'': 59, ''odi'': 60, ''odj'': 61, ''odg'': 62, ''odh'': 63, ''obe'': 64, ''obf'': 65, ''obc'': 66, ''obd'': 67, ''obi'': 68, ''obj'': 69, ''obg'': 70, ''obh'': 71, ''ob6'': 72, ''ob7'': 73, ''ob4'': 74, ''ob5'': 75, ''oba'': 76, ''obb'': 77, ''ob8'': 78, ''ob9'': 79, ''oay'': 80, ''oaz'': 81, ''oaw'': 82, ''oax'': 83, ''ob2'': 84, ''ob3'': 85, ''ob0'': 86, ''ob1'': 87, ''oaq'': 88, ''oar'': 89, ''oao'': 90, ''oap'': 91, ''oau'': 92, ''oav'': 93, ''oas'': 94, ''oat'': 95, ''oca'': 96, ''ocb'': 97, ''oc8'': 98, ''oc9'': 99 }


Esta es una adaptación de Clojure del código de Buho y Julie que debería funcionar tanto con las nuevas Hojas de cálculo de Google como con las Hojas de cálculo de Google heredadas.

(defn wid->gid [wid] (let [new-wid? (> (.length wid) 3) wid (if new-wid? (.substring wid 1) wid) xor-val (if new-wid? 474 31578)] (bit-xor (Integer/parseInt wid 36) xor-val))) (defn gid->wid [gid] (let [new-gid? (> gid 31578) xor-val (if new-gid? 474 31578) letter (if new-gid? "o" "")] (str letter (Integer/toString (bit-xor gid xor-val) 36))))


Esta es una adaptación de Java del código de Buho que funciona con las nuevas Hojas de cálculo de Google y con las Hojas de cálculo de Google heredadas.

// "od4" to 2 (legacy style) // "ogtw0h0" to 1017661118 (new style) public static int widToGid(String worksheetId) { boolean idIsNewStyle = worksheetId.length() > 3; // if the id is in the new style, first strip the first character before converting worksheetId = idIsNewStyle ? worksheetId.substring(1) : worksheetId; // determine the integer to use for bitwise XOR int xorValue = idIsNewStyle ? 474 : 31578; // convert to gid return Integer.parseInt(worksheetId, 36) ^ xorValue; } // Convert 2 to "od4" (legacy style) // Convert 1017661118 to "ogtw0h0" (new style) public static String gidToWid(int gid) { boolean idIsNewStyle = gid > 31578; // determine the integer to use for bitwise XOR int xorValue = idIsNewStyle ? 474 : 31578; // convert to worksheet id, prepending ''o'' if it is the new style. return idIsNewStyle ? ''o'' + Integer.toString((worksheetIndex ^ xorValue), 36): Integer.toString((worksheetIndex ^ xorValue), 36); }


Este código funciona con las nuevas hojas de Google.

// Conversion of Worksheet Ids to GIDs and vice versa // od4 > 2 function wid_to_gid(wid) { var widval = wid.length > 3 ? wid.substring(1) : wid; var xorval = wid.length > 3 ? 474 : 31578; return parseInt(String(widval), 36) ^ xorval; } // 2 > od4 function gid_to_wid(gid) { var xorval = gid > 31578 ? 474 : 31578; var letter = gid > 31578 ? ''o'' : ''''; return letter + parseInt((gid ^ xorval)).toString(36); }


No puedo agregar un comentario a la publicación de Wasilewski porque aparentemente no tengo reputación, así que aquí están las dos funciones de conversión en Javascript basadas en la respuesta de Wasilewski:

// Conversion of Worksheet Ids to GIDs and vice versa // od4 > 2 function wid_to_gid(wid) { return parseInt(String(wid),36)^31578 } // 2> 0d4 function gid_to_wid(gid) { // (gid xor 31578) encoded in base 36 return parseInt((gid^31578)).toString(36); }


Si estás usando Python con gspread , esto es lo que haces:

wid = worksheet.id widval = wid[1:] if len(wid) > 3 else wid xorval = 474 if len(wid) > 3 else 31578 gid = int(str(widval), 36) ^ xorval

Probablemente abriré un PR para esto.