Как обновить вложения ссылок с помощью smartsheet c # sdk? - PullRequest
0 голосов
/ 04 ноября 2019

У меня есть смарт-лист с кучей вложений ссылок. Я хотел бы заменить начало ссылок другой подстрокой. Я новичок в smartsheet и не смог найти много примеров кода.

Настройка листа,

SmartsheetClient smartsheet = new SmartsheetBuilder().SetAccessToken(accessToken).Build();

PaginatedResult<Sheet> sheets = smartsheet.SheetResources.ListSheets(
    null,               // IEnumerable<SheetInclusion> includes
    null,               // PaginationParameters
    null                // Nullable<DateTime> modifiedSince = null
);

long sheetId = (long)sheets.Data[1].Id; //get first sheet's ID

//get sheet associated with sheetId
Sheet sheet = smartsheet.SheetResources.GetSheet(sheetId, null, null, null, null, null, null, null);

Здесь я пытаюсь создать список строк, содержащих ссылки, которые должныизменить,

List<Row> rowsToUpdate = new List<Row>();

foreach (var row in sheet.Rows)
{
    Row rowToUpdate = null;
    var cellsToUpdate = new List<Cell>();
    var at = row.Attachments;

    string toReplace = "http://blahblah/blah";
    string newValue = "https://hello/";

    foreach(var attachment in row.Attachments)
    {
        var url = attachment.Url;
        var id = attachment.Id;
        if (url!=null && url.Contains(toReplace)){
            var newUrl = url.Replace(toReplace, newValue);
        }
    }

    //create new cell that will contain updated links
    var cellToUpdate = new Cell {
    ColumnId = , //is it possible to get Attachments column Id? 
    Value = //add updated links here?
    };

    cellsToUpdate.Add(cellToUpdate);

    //this will be added to the list rowsToUpdate
    rowToUpdate = new Row {
    Id = row.Id,
    Cells = cellsToUpdate
    };
}


smartsheet.SheetResources.RowResources.UpdateRows(sheet.Id.Value, rowsToUpdate);           

Я попытался получить идентификатор столбца вложения, но этот метод не сработал, потому что я думаю, что это основной столбец (?)

var columns = smartsheet.SheetResources.ColumnResources.ListColumns(sheetId, null, null).Data;
var attachmentCol = columns[0].Title;

Спасибо за вашу помощь.

1 Ответ

2 голосов
/ 05 ноября 2019

Вложения в строке не обозначаются как столбцы, а являются отдельным атрибутом строки. Когда вы звоните GetSheet, вы захотите включить в ответ вложения:

Sheet sheet = smartsheet.SheetResources.GetSheet(sheetId, new List<SheetLevelInclusion> { SheetLevelInclusion.ATTACHMENTS }, 
                null, null, null, null, null, null);

Чтобы обновить URL, вам, возможно, придется удалить вложение:

smartsheet.SheetResources.AttachmentResources.DeleteAttachment(
  9283173393803140,           // long sheetId
  7169782752536452            // long attachmentId
);

, а затем повторнодобавьте ваше измененное вложение:

Attachment attachmentSpecification = new Attachment
{
  Url = "http://www.google.com",
  AttachmentType = AttachmentType.LINK,
  Name = "Search Engine"
};

// Attach URL to row
Attachment updatedAttachment = smartsheet.SheetResources.RowResources.AttachmentResources.AttachUrl(
  9283173393803140,           // long sheetId
  0123456789012345,           // long rowId
  attachmentSpecification
);
...