Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用gsheet api和javascript更新范围内的特定单元格_Javascript_Google Sheets Api - Fatal编程技术网

使用gsheet api和javascript更新范围内的特定单元格

使用gsheet api和javascript更新范围内的特定单元格,javascript,google-sheets-api,Javascript,Google Sheets Api,我有一个纯javascript前端webapp,我已经设法连接到googlesheetapi,从中获取值,现在我想更新选定的单元格集。我指的是可用的代码。代码粘贴在下面。这是工作,但它是直接覆盖现有的数据顺序,我希望它执行选择性批量更新- 例如: 范围A:B有数据 1 Apple 2 Orange 3 Pineapple 在运行更新函数时,我希望得到以下结果 1 Apple 2 Pear 3 Pineapple 所以只需更新第2行,而不是重写。目前,我在gsheet上

我有一个纯javascript前端webapp,我已经设法连接到googlesheetapi,从中获取值,现在我想更新选定的单元格集。我指的是可用的代码。代码粘贴在下面。这是工作,但它是直接覆盖现有的数据顺序,我希望它执行选择性批量更新-

例如: 范围A:B有数据

1   Apple
2   Orange
3   Pineapple
在运行更新函数时,我希望得到以下结果

1   Apple
2   Pear
3   Pineapple
所以只需更新第2行,而不是重写。目前,我在gsheet上获得以下输出

2   Pear
2   Orange
3   Pineapple
如果有人能帮我,那就太好了。如果有任何其他功能,我可以用来更新这个请建议。基本上

我的代码是

 function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
    // [START sheets_batch_update_values]
    var values = [
      [
        [usedIdArray],
      ],

    ];
    // [START_EXCLUDE silent]
    values = _values;
    // [END_EXCLUDE]
    var data = [];
    data.push({
      range: range,
      values: values,
      majorDimension: "COLUMNS"
    });
    // Additional ranges to update.

    var body = {
      data: data,
      valueInputOption: valueInputOption

    };
    gapi.client.sheets.spreadsheets.values.batchUpdate({
      spreadsheetId: spreadsheetId,
      resource: body
    }).then((response) => {
      var result = response.result;
      console.log(`${result.totalUpdatedCells} cells updated.`);
      // [START_EXCLUDE silent]
      callback(response);
      // [END_EXCLUDE]
    });
    // [END sheets_batch_update_values]
  }

我相信你的目标和情况如下

const spreadsheetId = "###";  // Spreadsheet ID
const range = "Sheet1!B2:C";  // Range: In this case, the header row is not included.
const _values = {"2": "reader", "4": "gloom", "8": "jest", "10": "honor", "11": "contribution", "12": "session", "13": "money", "14": "opinion", "15": "steward", "16": "runner", "17": "feminist"};  // Your input values.
const valueInputOption = "USER_ENTERED";
function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
  gapi.client.sheets.spreadsheets.values.get({spreadsheetId: spreadsheetId, range: range})
  .then(function(response) {
    let values = response.result.values;
    values.forEach(e => {
      if (_values[e[0]]) e[1] = _values[e[0]];
    });

    // If the input ID is not existing in the Spreadsheet, the ID and value is appended using this section.
    const obj = values.reduce((o, [b]) => Object.assign(o, {[b]: true}), {});
    const r = Object.entries(_values).filter(([k]) => !obj[k]);
    values = values.concat(r);

    var data = [];
    data.push({range: range, values: values});
    var body = {data: data, valueInputOption: valueInputOption};
    gapi.client.sheets.spreadsheets.values.batchUpdate({spreadsheetId: spreadsheetId,resource: body})
    .then((response) => {
      var result = response.result;
      console.log(`${result.totalUpdatedCells} cells updated.`);
      callback(response);
    });
  }, function(reason) {
    console.error('error: ' + reason.result.error.message);
  });
}
  • 当表1中“B”列的键包括输入值的键时,您希望替换“C”列的值
  • 当Sheet1中“B”列的键不包括输入值的键时,您希望将输入值附加到Sheet1
  • 您希望使用googleapis for Javascript实现这一点
  • 您已经使用Sheets API获取并输入了Google电子表格的值
为了实现上述目标,我想提出以下修改

修改点:
  • 在脚本中,输入的值直接放入电子表格。这样,就不会替换现有值
  • 为了实现您的目标,我想提出以下流程。
  • 使用values.get in Sheets API方法从电子表格中的“Sheet1”检索现有值
  • 使用检索到的值创建更新的值
  • 使用脚本将更新的值放入“Sheet1”
当此流反映到脚本中时,它将变成如下所示

const spreadsheetId = "###";  // Spreadsheet ID
const range = "Sheet1!B2:C";  // Range: In this case, the header row is not included.
const _values = {"2": "reader", "4": "gloom", "8": "jest", "10": "honor", "11": "contribution", "12": "session", "13": "money", "14": "opinion", "15": "steward", "16": "runner", "17": "feminist"};  // Your input values.
const valueInputOption = "USER_ENTERED";
function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
  gapi.client.sheets.spreadsheets.values.get({spreadsheetId: spreadsheetId, range: range})
  .then(function(response) {
    let values = response.result.values;
    values.forEach(e => {
      if (_values[e[0]]) e[1] = _values[e[0]];
    });

    // If the input ID is not existing in the Spreadsheet, the ID and value is appended using this section.
    const obj = values.reduce((o, [b]) => Object.assign(o, {[b]: true}), {});
    const r = Object.entries(_values).filter(([k]) => !obj[k]);
    values = values.concat(r);

    var data = [];
    data.push({range: range, values: values});
    var body = {data: data, valueInputOption: valueInputOption};
    gapi.client.sheets.spreadsheets.values.batchUpdate({spreadsheetId: spreadsheetId,resource: body})
    .then((response) => {
      var result = response.result;
      console.log(`${result.totalUpdatedCells} cells updated.`);
      callback(response);
    });
  }, function(reason) {
    console.error('error: ' + reason.result.error.message);
  });
}
修改脚本: 参考:
补充: 从您的回复和共享的电子表格,我可以理解如下

  • 在“B”列中有ID,在“C”列中有值
  • 当ID为1到17时,您需要替换并输入如下值。
    • 2读者4悲观8笑话10荣誉11贡献12会议13金钱14意见15管家16跑步者17女权主义者
    • 在上面的例子中,2,4,8,,,是ID。字符串值是要输入的值
示例脚本: 从您的回复中,我无法理解您期望的输入值。因此,在这个示例脚本中,我从repling中的值中提出了输入值

在这种情况下,函数
batchUpdateValues
的输入值如下所示

const spreadsheetId = "###";  // Spreadsheet ID
const range = "Sheet1!B2:C";  // Range: In this case, the header row is not included.
const _values = {"2": "reader", "4": "gloom", "8": "jest", "10": "honor", "11": "contribution", "12": "session", "13": "money", "14": "opinion", "15": "steward", "16": "runner", "17": "feminist"};  // Your input values.
const valueInputOption = "USER_ENTERED";
function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
  gapi.client.sheets.spreadsheets.values.get({spreadsheetId: spreadsheetId, range: range})
  .then(function(response) {
    let values = response.result.values;
    values.forEach(e => {
      if (_values[e[0]]) e[1] = _values[e[0]];
    });

    // If the input ID is not existing in the Spreadsheet, the ID and value is appended using this section.
    const obj = values.reduce((o, [b]) => Object.assign(o, {[b]: true}), {});
    const r = Object.entries(_values).filter(([k]) => !obj[k]);
    values = values.concat(r);

    var data = [];
    data.push({range: range, values: values});
    var body = {data: data, valueInputOption: valueInputOption};
    gapi.client.sheets.spreadsheets.values.batchUpdate({spreadsheetId: spreadsheetId,resource: body})
    .then((response) => {
      var result = response.result;
      console.log(`${result.totalUpdatedCells} cells updated.`);
      callback(response);
    });
  }, function(reason) {
    console.error('error: ' + reason.result.error.message);
  });
}
函数
batchUpdateValues
如下所示

const spreadsheetId = "###";  // Spreadsheet ID
const range = "Sheet1!B2:C";  // Range: In this case, the header row is not included.
const _values = {"2": "reader", "4": "gloom", "8": "jest", "10": "honor", "11": "contribution", "12": "session", "13": "money", "14": "opinion", "15": "steward", "16": "runner", "17": "feminist"};  // Your input values.
const valueInputOption = "USER_ENTERED";
function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
  gapi.client.sheets.spreadsheets.values.get({spreadsheetId: spreadsheetId, range: range})
  .then(function(response) {
    let values = response.result.values;
    values.forEach(e => {
      if (_values[e[0]]) e[1] = _values[e[0]];
    });

    // If the input ID is not existing in the Spreadsheet, the ID and value is appended using this section.
    const obj = values.reduce((o, [b]) => Object.assign(o, {[b]: true}), {});
    const r = Object.entries(_values).filter(([k]) => !obj[k]);
    values = values.concat(r);

    var data = [];
    data.push({range: range, values: values});
    var body = {data: data, valueInputOption: valueInputOption};
    gapi.client.sheets.spreadsheets.values.batchUpdate({spreadsheetId: spreadsheetId,resource: body})
    .then((response) => {
      var result = response.result;
      console.log(`${result.totalUpdatedCells} cells updated.`);
      callback(response);
    });
  }, function(reason) {
    console.error('error: ' + reason.result.error.message);
  });
}

为了正确复制您的情况,我可以问一下
range
usedIdArray
的值吗?嘿@Tanaike谢谢您的快速回复。当然-范围是1!B:C和usedIdArray是B列中的ID数组,我使用函数getValues(spreadsheetId,range,callback)从同一张表中收集了这些ID。谢谢您的快速回复。从你的回复中,我想确认你的目标。在电子表格中,“B”和“C”列分别具有ID和值。您希望使用具有ID和值的输入值替换电子表格上的ID值。我的理解正确吗?嘿@Tanaike你完全正确-B列有键,C列有值。在我的getValues请求中,我提取了所有在相邻的C列中没有值的键,在更新它们之前,我有一个数组,该数组中有要针对这些键添加的值。先生,你说得很对,谢谢你的回答。从你的回复中,我提出了一个修改后的脚本作为答案。你能确认一下吗?如果我误解了你的目标,而这不是你期望的方向,我道歉。届时,您能否提供实现目标的详细信息。Tanks lot@Tanaike。我将期待实现这一点,然后返回给您:)。从表面上看,这似乎是我想要的解决方案。我一定会回复你的。非常感谢您如此迅速的回复。@pc372谢谢您的回复。首先,当您测试这个脚本时,请针对简单的情况进行测试。因为根据我的理解,我测试了这个简单的场景,我不确定你的整个脚本。请注意这一点。嘿@Tanike-解决方案也运行良好-只是它覆盖了已经写入的数据。我认为这一行应该是“const obj=values.reduce((o[b],I)=>Object.assign(o,{[b]:I}),{}”);”我应该在这里使用我自己的数组而不是“o”吗?例如,现在如果要添加32个新行,它可以完美地写入16行,然后在前面的16行上覆盖下一个16行。@pc372感谢您的回复。对于给您带来的不便,我深表歉意,也为我糟糕的英语水平表示歉意。很遗憾,我无法理解你的答复。因此,为了正确理解您的目标,您能否提供包含预期输入和输出的示例电子表格?通过这个,我想确认它并修改脚本。