从另一张工作表中提取数据后,如何反转Google工作表中列的顺序 修改点:修改后的脚本:参考文献:

问题描述

因此,我成功使用Google表格API将数据从一个Google表格中提取到了另一个Google表格中。现在,我想反转列的顺序,例如,想要A的列为K,B的列为J,等等。这是我现在的代码:

function authenticate() {
return gapi.auth2.getAuthInstance()
.signIn({scope: "https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/spreadsheets"})
.then(function() { console.log("Sign-in successful"); },function(err) { console.error("Error signing in",err); });
function loadClient() {
gapi.client.setApiKey("");
.then(function() { console.log("GAPI client loaded for API"); },function(err) { console.error("Error loading GAPI client for API",err); });
  // Make sure the client is loaded and sign-in is complete before calling this method.
function execute() {
return gapi.client.sheets.spreadsheets.sheets.copyTo({
  "spreadsheetId": "","sheetId":,"resource": {
    "destinationSpreadsheetId": ""
  }
})
.then(function(response) {
            // Handle the results here (response.result has the parsed body).
            console.log("Response",response);
          },function(err) { console.error("Execute error",err); });
("client:auth2",function() {
gapi.auth2.init({client_id: ""});
});

如何编辑此代码以使用代码将列反向排列?这是我的电子表格的链接:https://docs.google.com/spreadsheets/d/1BJ0Y8viJE8a2jU6rfNGuNp5n1jwQauee1p9-PxuuVHE/edit#gid=533169457

解决方法

我相信您的目标和现状如下。

  • 您要通过使用googleapis for Javascript从源工作表中选择特定列,从而将源工作表中的值放到目标工作表中。
    • 以您的情况为例,您想从源工作表中选择“ A,K,B,J”列并将其放入目标工作表中。
  • 您已经使用当前脚本通过Sheets API获取和放置了Google Spreadsheet的值。

修改点:

  • 为了实现您的目标,我想提出以下流程。
    1. 使用sheetsheets.values.get从源工作表中检索值。
    2. 从检索到的值中提取特定的列。
    3. 使用sheetssheets.values.update将提取的值放入目标表。

当此流程反映到您的脚本时,它如下所示。

修改后的脚本:

在使用此脚本之前,请设置变量spreadsheetIdsourceSheetNamedestinationSheetNameextractColumns

const spreadsheetId = "###";
const sourceSheetName = "###";
const destinationSheetName = "###";
const extractColumns = [1,11,2,10];  // "A,K,B,J" in order.

const transpose = (ar) => ar[0].map((_,i) => ar.map(r => r[i]));
gapi.client.sheets.spreadsheets.values.get({
  spreadsheetId: spreadsheetId,range: sourceSheetName
}).then((res)=>{
  const transposedValues = transpose(res.result.values);
  const extractedColumns = extractColumns.map(c => transposedValues[c - 1]);
  const resultValues = transpose(extractedColumns);
  gapi.client.sheets.spreadsheets.values.update({
    spreadsheetId: spreadsheetId,range: destinationSheetName,valueInputOption: "USER_ENTERED"
  },{
    values: resultValues
  }).then((r) => {
    console.log(res.result.values)
  },function(er) {
    console.error(er.result.error.message);
  })
},function(err) {
  console.error(err.result.error.message);
});

参考文献:

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...