I believe your goal and situation as follows.
- You want to replace the values of the column "C" when the keys of the column "B" in Sheet1 include the keys of the inputted values.
- You want to append the inputted values to Sheet1 when the keys of the column "B" in Sheet1 doesn't include the keys of the inputted values.
- You want to achieve this using googleapis for Javascript.
- You have already get and put values for Google Spreadsheet using Sheets API.
In order to achieve above, I would like to propose the following modification.
Modification points:
- In your script, the inputted values are directly put to the Spreadsheet. By this, the existing values are not replaced.
- In order to achieve your goal, I would like to propose the following flow.
- Retrieve the existing values from "Sheet1" in the Spreadsheet using the method of values.get in Sheets API.
- Create the updated values using the retrieved values.
- Put the updated values to "Sheet1" using your script.
When this flow is reflected to your script, it becomes as follows.
Modified script:
function batchUpdateValues(spreadsheetId, range, valueInputOption, _values, callback) {
// 1. Retrieve the existing values from "Sheet1" in the Spreadsheet using the method of values.get in Sheets API.
gapi.client.sheets.spreadsheets.values.get({spreadsheetId: spreadsheetId, range: range})
.then(function(response) {
// 2. Create the updated values using the retrieved values.
let values = response.result.values;
const obj = values.reduce((o, [b], i) => Object.assign(o, {[b]: i}), {});
const addValues = _values.reduce((ar, [b, c]) => {
if (obj[b]) {
values[obj[b]][1] = c;
} else {
ar.push([b, c]);
}
return ar;
}, []);
values = values.concat(addValues);
// 3. Put the updated values to "Sheet1" using your script.
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);
});
}
Reference:
Added:
From your replying and shared Spreadsheet, I could understand like below.
- Thare is IDs in the column "B" and values in the column "C".
- When the IDs are 1 to 17, you want to replace and put the values like below.
- 2 reader 4 gloom 8 jest 10 honor 11 contribution 12 session 13 money 14 opinion 15 steward 16 runner 17 feminist
- In above case, 2, 4, 8,,, is the IDs. The string values are the values you want to put.
Sample script:
From your replying, I cannot understand about the input values you expect. So in this sample script, I propose the input values from the values in your repling.
In this case, the input values for the function batchUpdateValues is as follows.
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";
The function batchUpdateValues is as follows.
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);
});
}
rangeandusedIdArray?