gpt4 book ai didi

google-apps-script - 将邮政编码从 Google 电子表格导入 Google 融合表时,如何防止删除前导零?

转载 作者:行者123 更新时间:2023-12-02 21:04:52 35 4
gpt4 key购买 nike

我有一个 Google 表单,用户可以在其中输入邮政编码。表单响应被记录到 Google 电子表格中,然后与 Google Fusion Table 同步以进行地理编码到 map 上。邮政编码在电子表格中以文本形式表示,以保留前导零,但当同步到融合表时,零将被删除。当电子表格同步到融合表时,是否有办法保留列格式,以便不删除前导零?

最佳答案

根据这些指令,有一个名为 sync() 的函数。请参阅下面的代码:因此,我假设这是正在执行工作的代码。这对于评论部分来说太大了,所以我将其发布在答案中,否则我必须编辑您的问题。因此,请在评论中告诉我这是否是代码。

/**
* Syncs the Fusion Table to the form data. Run this every hour or so.
*/
function sync() {
init();

// Get the data in the spreadsheet and convert it to a dictionary.
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var spreadsheetData = sheet.getRange(1, 1, lastRow, lastColumn);
var spreadsheetValues = spreadsheetData.getValues();
var columns = spreadsheetValues[0];
var spreadsheetMap = mapRowsByRowId(columns,
spreadsheetValues.slice(1, spreadsheetValues.length));

// Get the columns in the spreadsheet and escape any single quotes
var escapedColumns = [];
for (var i = 0; i < columns.length; i++) {
var columnName = columns[i];
columnName = escapeQuotes(columnName);
escapedColumns.push(columnName);
if (column === ADDRESS_COLUMN) {
escapedColumns.push(escapedQuotes(LOCATION_COLUMN));
}
}

// Get the data from the table and convert to a dictionary.
var query = "SELECT '" + escapedColumns.join("','") + "' FROM " + DOCID;
var ftResults = runSqlWithRetry(query);
if (!ftResults) {
return;
}
var ftMap = mapRowsByRowId(ftResults.columns, ftResults.rows);

// For each row in the Fusion Table, find if the row still exists in the
// spreadsheet. If it exists, make sure the values are the same. If
// they are different, update the Fusion Table data.
// If the row doesn't exist in the spreadsheet, delete the row from the table.
for (var rowId in ftMap) {
var spreadsheetRow = spreadsheetMap[rowId];
if (spreadsheetRow) {
var updates = [];
var tableRow = ftMap[rowId];

for (var column in tableRow) {
if (column === 'rowid') {
continue;
}
var tableValue = tableRow[column];
var spreadsheetValue = spreadsheetRow[column];
if (tableValue != spreadsheetValue) {
spreadsheetValue = processSpreadsheetValue(column, spreadsheetValue, updates, true);
}
}

// If there are updates, send the UPDATE query.
if (updates.length) {
var query = [];
query.push('UPDATE ');
query.push(DOCID);
query.push(' SET ');
query.push(updates.join(','));
query.push(" WHERE rowid = '");
query.push(rowId);
query.push("'");
runSqlWithRetry(query.join(''));
waitBetweenCalls();
}

} else {
// If the row doesn't exist in the spreadsheet, delete it from the table
runSqlWithRetry('DELETE FROM ' + DOCID + " WHERE rowid = '" +
rowId + "'");
waitBetweenCalls();
}
}

// Insert all the data into the Fusion Table that failed to insert.
// These rows were given a rowid of -1 or have a blank rowid.
var failedInserts = spreadsheetMap[-1];
for (var i = 0; failedInserts && i < failedInserts.length; i++) {
var rowId = createRecord(failedInserts[i]);
if (!rowId) {
rowId = -1;
}
insertRowId(rowId, failedInserts[i].spreadsheetRowNum);
waitBetweenCalls();
}
}

关于google-apps-script - 将邮政编码从 Google 电子表格导入 Google 融合表时,如何防止删除前导零?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28073117/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com